我有两个ObservableCollection<string>
类型的属性(在不同的项目中);我想要做的是使用反射和SetBinding
将这两个绑定起来,就像这样 -
//Get the PropertyDescriptor for first collection property
PropertyDescriptor relatedPropertyDesc = prop.Find(firstCollPropName, false);
Binding relatedPropBinding = new Binding(relatedPropertyDesc.Name);
relatedPropBinding.Source = this.SelectedItem;
relatedPropBinding.Mode = BindingMode.TwoWay;
//Bind the second collection property using binding created above
propItem.SetBinding(MyItem.SecondCollProperty, relatedPropBinding);
然后将此 SecondCollProperty
绑定到ComboBox的 ItemsSource
中。
因此,这样可以正确地工作,firstCollProperty中存在的值在组合框中正确显示; 但是,如果在运行时对firstCollProperty进行了一些更改,则不会在ComboBox中反映出来!(添加新项或创建新集合对象)。
刷新绑定后,更改将被正确反映(再次执行上面的代码)。
我的问题是 - 如果两个 ObservableCollections
彼此绑定,为什么第一个的任何更改都不会反映在另一个上?但是对于字符串或双精度类型的属性,同样的事情起作用。
有没有办法实现这一点?