简体   繁体   English

选择值后,ComboBox ItemsSource绑定未更新

[英]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. 我在WPF中有一个ComboBox,将其ItemsSource属性绑定到返回String的IEnumerable的属性。 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. 包含ComboBox数据的类实现INotifyPropertyChanged接口,并在属性更新后立即调用OnPropertyChanged(..)。 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. 但是,一旦将ComboBox展开一次或选择一个值,ItemsSource集合中的更改就不再更新。 What may be the reason for this behaviour? 此行为的原因可能是什么?

Heres the XAML 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: DataContext在后面的代码中设置:

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. 使用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. 该资源将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). 最后,我使用ObservableCollection实现了Binding属性,该属性在添加新的Packet时跟踪密钥(因此,字典的每个密钥在该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. 我认为这并不是一个令人满意的解决方案(因为您必须独立跟踪两个Collections),但是它的工作原理是这样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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