开发者

Binding two Observable Collections with each other

开发者 https://www.devze.com 2023-01-24 22:01 出处:网络
I have two properties of ObservableCollection<string> type (in separate projects); What I want to do is to bind these two using reflection and SetBinding like this -

I have two properties of ObservableCollection<string> type (in separate projects); What I want to do is to bind these two using reflection and SetBinding like this -

//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);

This SecondCollProperty is then bound to a ComboBox's ItemsSource.

As such this works correctly, values present in firstCollProperty are displayed correctly in combobox; but if some changes are made in firstCollProperty at run time then they are not reflected in ComboBox!(adding new items or creating new collection object).

Changes are reflected correctly after refreshing the binding(again executing the above code).

My question is - If two ObservableCollections are binded together why any changes in first doesn't get reflected in o开发者_C百科ther? but same thing works for properties of string or double type.

Is there any way of achieving this?


Just going through some old unanswered questions and saw this. Undoubtedly you've come up with a workaround by now, but my recommendation would be look into something like CLinq, Bindable Linq, or Obtics for this. See this question for more details. You'd take the first collection, create a dynamic query against it, and expose that dynamic query (which implements IObservableCollection) as your second property.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号