akj*_*shi 6 data-binding reflection silverlight wpf observablecollection
我有两个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);
Run Code Online (Sandbox Code Playgroud)
这SecondCollProperty是然后绑定到一个ComboBox的ItemsSource。
因此,这可以正常工作, firstCollProperty 中存在的值在组合框中正确显示;但是如果在运行时在 firstCollProperty 中进行了一些更改,则它们不会反映在 ComboBox 中!(添加新项目或创建新的集合对象)。
刷新绑定(再次执行上面的代码)后,更改会正确反映。
我的问题是 -如果将两个ObservableCollections绑定在一起,为什么第一个中的任何更改不会反映在其他中?但同样的事情适用于 string 或 double 类型的属性。
有没有办法实现这一目标?