Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a ComboBox in WPF binding its ItemsSource Property to a Property returning an IEnumerable of String. The binding is just one-way. The class that contains the data for the ComboBox implements INotifyPropertyChanged Interface and calls the OnPropertyChanged(..) as soon as the Property gets updated. When I leave the ComboBox untouched the changes are correctly propagated. But as soon as the ComboBox is expanded once or a value is selected the changes in the ItemsSource Collection are no longer updated. What may be the reason for this behaviour?

Heres the XAML

<ComboBox Name="cmbSourceNames" Grid.Row="0" SelectedItem="{Binding Path=CurrentSource, UpdateSourceTrigger=PropertyChanged}"
  ItemsSource="{Binding Path=SourceAddresses, NotifyOnSourceUpdated=True}"/>

The DataContext is set in the code behind:

this.cmbSourceNames.DataContext = this._dpa;

And this one is the Method that triggers the change of the Property. The Method for adding the Packet is delegated to the Current Dispatcher with BeginInvoke.

    private void DispatcherAddDataPacket(DataPacket dp)
    {
        ObservableCollection<DataPacket> dpList;
        this._dpkts.TryGetValue(dp.SourceAddress, out dpList);
        if (dpList == null)
        {
            dpList = new ObservableCollection<DataPacket>();
            dpList.Add(dp);
            this._dpkts.Add(dp.SourceAddress, dpList);
            OnPropertyChanged("SourceAddresses");
        }
        else
        {
            dpList.Add(dp);
        }
    }

The Property is giving back the Keys of the Dictionary as IEnumerable.

share|improve this question
    
Please post the XAML. –  Michael Perrenoud Jun 12 '13 at 18:54
    
Post your binding code. Its better to bind selectedIndex to a property in viewmodel –  Jasti Jun 12 '13 at 19:18
    
Made an edit with the XAML and some code snippets –  Björn Höper Jun 12 '13 at 20:19
    
@Jasti: I can imagine that but in this case it is a little bit more convenient because I may use the binding directly as the dictionary key –  Björn Höper Jun 12 '13 at 20:20
    
@Jasti: I also tried the code with the Binding to SelectedItem completely removed. But this did not change anything. –  Björn Höper Jun 13 '13 at 4:42

1 Answer 1

Finally I implemented the Binding Property using an ObservableCollection tracking the keys when a new Packet gets added (so every key to the Dictionary has an equivalent in this ObservableCollection). I think it's not really a satisfying solution (because you have to keep track of both Collections independently) but it works like this.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.