我有一个可观察的集合...... SelectableDataContext<T>
..在泛型类SelectableDataContext<T>
中......有两个私有成员变量
- 私人 T 项目。
- 私有 bool isSelected。
当 IsSelected 属性更改时...我的集合的更改属性未触发。
我认为它应该开火……因为它Reset
在INotifyCollectionChangedAction
.
我有一个可观察的集合...... SelectableDataContext<T>
..在泛型类SelectableDataContext<T>
中......有两个私有成员变量
当 IsSelected 属性更改时...我的集合的更改属性未触发。
我认为它应该开火……因为它Reset
在INotifyCollectionChangedAction
.
这是一个老问题,但为了任何可能像我一样通过搜索遇到这个问题的人的利益:
NotifyCollectionChangedAction.Reset
意思是“收藏的内容发生了巨大的变化”。引发 Reset 事件的一种情况是调用Clear()
底层可观察集合时。
使用 Reset 事件,您不会在参数中获得NewItems
andOldItems
集合。NotifyCollectionChangedEventArgs
这意味着您最好使用事件的“发送者”来获取对修改后的集合的引用并直接使用它,即假设它是一个新列表。
这方面的一个例子可能是这样的:
((INotifyCollectionChanged)stringCollection).CollectionChanged += new NotifyCollectionChangedEventHandler(StringCollection_CollectionChanged);
...
void StringCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (string s in e.NewItems)
{
InternalAdd(s);
}
break;
case NotifyCollectionChangedAction.Remove:
foreach (string s in e.OldItems)
{
InternalRemove(s);
}
break;
case NotifyCollectionChangedAction.Reset:
ReadOnlyObservableCollection<string> col = sender as ReadOnlyObservableCollection<string>;
InternalClearAll();
if (col != null)
{
foreach (string s in col)
{
InternalAdd(s);
}
}
break;
}
}
这里有很多关于此重置事件的讨论:当清除 ObservableCollection 时,e.OldItems 中没有项目。
INotifyCollectionChanged
和之间有区别INotifyPropertyChanged
。
当对象中某个属性的值发生变化时,它应该使用INotifyPropertyChanged
接口实现通知其他人。
另一方面,当集合发生变化时,它应该让其他人知道使用number of items
实现。items themselves
INotifyCollectionChanged
现在,在您的情况下,您的集合中对象的属性值发生了变化。那应该引发PropertyChanged
事件,而不是CollectionChanged
事件。
当且仅当您通过添加新项目或从集合中删除现有项目来修改集合时,将触发集合更改。