将两个Observable Collections相互绑定

时间:2010-11-10 09:53:04

标签: wpf silverlight data-binding reflection observablecollection

我有两个ObservableCollection类型的属性(在单独的项目中);我想要做的是使用反射和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绑定在一起,为什么第一个中的任何更改都不会反映在其他?中,但同样适用于string或double类型的属性。

有没有办法实现这个目标?

1 个答案:

答案 0 :(得分:0)

刚刚经历了一些尚未解决的问题并看到了这一点。毫无疑问,你现在已经提出了一个解决方法,但我的建议是研究像CLinq,Bindable Linq或Obtics这样的东西。有关详细信息,请参阅this question。您将获取第一个集合,针对它创建动态查询,并将该动态查询(实现IObservableCollection)公开为您的第二个属性。