Silverlight:DependencyProperty (IList) 的双向绑定不起作用
嗯......它的工作原理,但只有当用户创建列表类型的属性时。
这是完整的场景。
我有一个模板化控件(带有复选框的多选组合框) 用户提供一个 List/ObservableCollection 作为其 ItemsSource。该集合可以是任何类型(员工、主席、人员等)。 ItemsSource 的 DependencyProperty 属于 IList 类型。
用户还可以在名为 SelectedItems 的 DependencyProperty 中提供具有双向绑定的列表(以便他可以显示某些已选中的项目并取回已选中的项目)
现在问题出在 SelectedItems dp 上。它在模板化控件中属于 IList 类型。 如果用户将其绑定到 List
,则双向绑定不起作用。 但如果将 List
更改为 List
我想不通到底是我哪里做得不对!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 TwoWay 绑定放置在
SelectedItems
属性上是没有意义的。这意味着控件应该创建一个实现IList
的对象实例并将其分配给源对象的属性。但是,控件无法知道要创建什么实际类型来分配给该属性。相反,您应该使用 OneWay 绑定到源对象中预先存在的列表(尽管是一个空列表)。然后,控件的任务就是简单地从
ItemsSource
属性中提供的列表中添加或删除该列表中的成员。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 implementingIList
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.