什么是notifycollectionchangedaction重置值
我有一个可观察的集合...SelectableDataContext
..并且在泛型类SelectableDataContext
中...具有两个私有成员变量
- Private T item。
- Private bool 被选中。
当 IsSelected 属性更改时...我的集合的更改属性不会触发。
我认为它应该触发...因为它是在 INotifyCollectionChangedAction
中的 Reset
。
I have an observable collection...SelectableDataContext<T>
..And in the generic class SelectableDataContext<T>
is...having two private member variables
- Private T item.
- Private bool isSelected.
When the IsSelected property changes...My collection's changed property is not firing .
I think it should fire...because it's Reset
in INotifyCollectionChangedAction
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个老问题,但为了任何可能像我一样通过搜索遇到这个问题的人的利益:
NotifyCollectionChangedAction.Reset
意味着“集合的内容发生了巨大的变化”。引发 Reset 事件的一种情况是当您对底层可观察集合调用Clear()
时。通过 Reset 事件,您不会在
NotifyCollectionChangedEventArgs
参数中获取NewItems
和OldItems
集合。这意味着您最好使用事件的“发送者”来获取对修改后的集合的引用并直接使用它,即假设它是一个新列表。
一个例子可能是这样的:
这里有很多关于此重置事件的讨论:清除 ObservableCollection 时,e.OldItems 中没有项目。
This is an old question but for the benefit of anyone who may come across this through a search as I did:
NotifyCollectionChangedAction.Reset
means "The content of the collection changed dramatically". One case where the Reset event is raised is when you callClear()
on the underlying observable collection.With the Reset event, you don't get the
NewItems
andOldItems
collections in theNotifyCollectionChangedEventArgs
parameter.This means you're better off using the "sender" of the event to get a reference to the modified collection and use that directly, i.e. assume it's a new list.
An example of this might be something like:
Lots of discussions on this Reset event here: When Clearing an ObservableCollection, There are No Items in e.OldItems.
INotifyCollectionChanged
和INotifyPropertyChanged
之间存在差异。当对象中的属性值发生更改时,应该使用 INotifyPropertyChanged 接口实现通知其他人。
另一方面,当集合中的
项目数量
或项目本身
发生变化时,应该使用INotifyCollectionChanged
实现让其他人知道。现在,在您的情况下,集合中对象的属性值发生变化。这应该引发
PropertyChanged
事件,而不是CollectionChanged
事件。There is a difference between
INotifyCollectionChanged
andINotifyPropertyChanged
.When a value of a propery in an object changes, it should notify others using
INotifyPropertyChanged
interface implementation.On the other hand, when
number of items
oritems themselves
change in a collection, it should let others know usingINotifyCollectionChanged
implementation.Now, in your case, value of a property of an object in your collection changes. That is supposed to raise
PropertyChanged
event, notCollectionChanged
event.当且仅当您通过添加新项目或从集合中删除现有项目来修改集合时,才会触发集合更改。
Collection changed will be fired if and only if you modify the collection that is via either Adding a new Item or Removing an existing item from the collection.