Rel*_*ity 15 wpf reset mvvm inotifycollectionchanged
我有一个可观察的集合...... SelectableDataContext<T>
在泛型类SelectableDataContext<T>
中......有两个私有成员变量
当IsSelected属性发生更改时...我的集合的已更改属性未触发.
我认为它应该解雇...因为它Reset
在INotifyCollectionChangedAction
.
G. *_*ard 37
这是一个古老的问题,但是为了任何可能通过搜索遇到这种情况的人的利益:
NotifyCollectionChangedAction.Reset
意思是"收藏内容发生了巨大变化".引发Reset事件的一种情况是调用Clear()
底层的observable集合.
使用Reset事件,您不会在参数中获取NewItems
和OldItems
集合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;
}
}
Run Code Online (Sandbox Code Playgroud)
这里有很多关于此重置事件的讨论:当清除ObservableCollection时,e.OldItems中没有项目.
归档时间: |
|
查看次数: |
15884 次 |
最近记录: |