简体   繁体   中英

ComboBox ItemsSource binding not updated after value selected

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM