组合框'itemssource中的更改未传递给selectedvalue的绑定

时间:2012-09-11 08:17:30

标签: c# wpf binding

我遇到了一些麻烦:

ComboBox cb = new ComboBox cb;
cb.ItemsSource = listItems; //ObservableCollection, contains objects with DependencyProperty "Name"
cb.DisplayMemberPath = "Name";
cb.SelectedValuePath = "Name";
Binding b = new Binding("NameOfItem");
b.Source = myObject;  //Has DependencyProperty "NameOfItem"
b.Mode = BindingMode.TwoWay;
cb.SetBinding(ComboBox.SelectedValueProperty, b);

当我在组合框中选择另一个值时,有界源“myobject”会更新。 但是当我只更改ItemsSource中项目的名称时,comboBox中显示的值确实会更新,但“myObject”没有得到通知,直到我在comboBox中重新选择该项。

1 个答案:

答案 0 :(得分:0)

将您的收藏品放入ObservableCollection<>并将你的组合框绑定到它。

ObservableCollection obc = new ObservableCollection(listItems);

Read common mistakes while using ObservableCollection