如何更新WPF ListView的源?
我有一个 WPF ListView 来绑定我的集合。该集合中对象的属性在后台线程中更改。当属性更改时,我需要更新 ListView。当我更改某些对象的属性时,不会触发 SourceUpdated 事件。
PS 将 ItemSource 设置为 null 并重新绑定是不合适的。
I have a WPF ListView to wich I bind my collection. Properties of the objects in this collection are changed in a background thread. I need to update ListView when properties are changed. SourceUpdated event is not fired when I change some object's property.
P.S. Setting ItemSource to null and rebinding then is not appropriate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该是自动的,您只需要使用 ObservableCollection 作为对象的容器,并且对象的类需要实现 INotifyPropertyChanged (您可以只实现要通知列表视图发生更改的属性的模式)
MSDN
that should be automatic, you just need to use an ObservableCollection as a container for your objects, and your object's class needs to implement INotifyPropertyChanged (you can just implement the pattern for the properties which you want to notify the listview that there was a change)
MSDN
确保您的对象实现 INotifyPropertyChanged 并在您的属性上调用 setter 时引发所需的更改通知。
如果您指的是从集合中添加/删除的项目(您没有提及),那么您需要确保您的集合是一个
ObservableCollection
Make sure your object implements
INotifyPropertyChanged
and raises the required change notification when the setter is called on your property.If you are instead referring to items being added/removed from the collection (which you did not mention) then you would need to make sure your collection is an
ObservableCollection<T>