2
votes

Well..its working BUT only when user creates a property that is of type List.

Here's the complete scenario.

I have a templated control (Multi Select ComboBox with Checkboxes) User gives a List/ObservableCollection as its ItemsSource. The collection can be of any type (Employee, Chair, Person etc). The DependencyProperty for ItemsSource is of Type IList.

User can also give a List with two-way binding in a DependencyProperty called SelectedItems (so that he can show some items as checked and get back the items which gets checked)

Now the issue is with the SelectedItems dp. It is of type IList in the templated control. The two-way binding does not work if user has bind it to, say, List<Person>. But it works if List<Person> is changed to List<object>.

I cant figure out what is it that I am not doing right!

1

1 Answers

1
votes

It doesn't make sense to place a TwoWay binding on the SelectedItems property. This would imply that the control should create and assign an instance of an object implementing IList to the property on the source object. However there is no way for control to know what actual type to create to assign to the property.

Instead you should be using a OneWay bind to a List that pre-exists in the source object albeit an empty one. The controls task then is to simply add or remove members in that list from the list supplied in the ItemsSource property.