[英]ComboBox ItemsSource binding not updated after value selected
我在WPF中有一個ComboBox,將其ItemsSource屬性綁定到返回String的IEnumerable的屬性。 綁定只是單向的。 包含ComboBox數據的類實現INotifyPropertyChanged接口,並在屬性更新后立即調用OnPropertyChanged(..)。 當我保持組合框不變時,更改將正確傳播。 但是,一旦將ComboBox展開一次或選擇一個值,ItemsSource集合中的更改就不再更新。 此行為的原因可能是什么?
XAML繼承人
<ComboBox Name="cmbSourceNames"
Grid.Row="0"
SelectedItem="{Binding Path=CurrentSource, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Path=SourceAddresses, NotifyOnSourceUpdated=True}"/>
DataContext在后面的代碼中設置:
this.cmbSourceNames.DataContext = this._dpa;
這就是觸發屬性更改的方法。 使用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);
}
}
該資源將IEnumerable的字典關鍵字歸還給您。
最后,我使用ObservableCollection實現了Binding屬性,該屬性在添加新的Packet時跟蹤密鑰(因此,字典的每個密鑰在該ObservableCollection中都具有等效項)。 我認為這並不是一個令人滿意的解決方案(因為您必須獨立跟蹤兩個Collections),但是它的工作原理是這樣的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.