绑定列表和 UI 控件,编辑时不更新
我以两种方式将 BindingList 绑定到列表框。绑定列表包含许多图像,这些图像显然仅在从绑定列表中添加或删除项目时才更新列表框。如何才能使绑定列表在修改项目时也引发 listchanged 事件?
编辑:我发现我遇到的问题是对象的属性没有被更改,而不是基础对象。
BindingList<ImageSource>();
但是,如果我这样做,这将不起作用:
BindingList<Image>();
然后将绑定路径设置为 Image.Source,它将正确更新,这是因为 Image 的属性已更改,但在第一个示例的情况下,只有 Image.Source 中的直接项目列表已更改。那么我怎样才能获得与第二个示例相同的行为呢?
最终编辑: 看来使用 ObservableCollection 而不是 BindingList 可以解决此问题。我的印象是,它们在通知集合变化方面是相同的。完整答案如下
I am binding a BindingList two way to a listbox. The Binding list contains a number of images which apparently only update the listbox if items are added or removed from the binding list. How can I make it so that the bindinglist also raises the listchanged event when an item is modified?
EDIT: I find the problem I am having is that a property of an object is not being changed, rather the base object.
BindingList<ImageSource>();
This wont work however if I did this:
BindingList<Image>();
And then set the binding path to Image.Source, it would update correctly and this is because a property of the Image has changed but in the case of the first example, only a direct item in the list has changed. So how may I get the same behaviour as the second example?
FINAL EDIT : It seems that using ObservableCollection instead of BindingList fixes this issue. I was under the impression that they were identical in notifying of changes in the collection. Full answer below
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该列表确实会引发该事件,但前提是底层项目通过
INotifyPropertyChanged
。The list does raise that event but only if the underlying items provides the proper notifications via
INotifyPropertyChanged
.BindingList 与 BindingList 不同。 microsoft.com/en-us/library/ms668604.aspx" rel="nofollow">ObservableCollection 中的 BindingList 不会通知其直接项已更改(除了当项目添加到集合或从集合中删除时)。然而,ObservableCollection 实现了 INotifyCollectionChanged 和 INotifyPropertyChanged 接口。这意味着对 ObservableCollection 的直接项的任何更改都会报告给 UI。
如果您使用绑定来定向项目并且需要更新项目而不是这些项目的属性,那么您似乎必须使用 ObservableCollection。另一种解决方案是从 BindingList 派生并实现 INotifyCollectionChanged。
我不是专家,但这是我在过去一个小时内收集到的内容,如果有人有任何需要添加或纠正的内容,请告诉我。
The BindingList differs from ObservableCollection in that BindingList does not notify that its direct items are changed (except when items are added or removed from the collection). ObservableCollection however implements INotifyCollectionChanged and INotifyPropertyChanged interfaces. This means that any change to direct items of an ObservableCollection are reported to the UI.
If you are using bindings to direct items and need to update items and not properties of those items, it seems that you have to use ObservableCollection. Another solution would be to derive from BindingList and implement INotifyCollectionChanged.
I am not an expert but this is what i have gathered during the last hour, if anyone has anything to add or correct please let me know.