栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

ObservableCollection不会注意到其中的Item更改的时间(即使使用INotifyPropertyChanged)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ObservableCollection不会注意到其中的Item更改的时间(即使使用INotifyPropertyChanged)

当您更改集合内的值时,不会调用ContentList的Set方法,而是应该注意CollectionChanged事件触发。

public class CollectionViewModel : ViewModelbase{   public ObservableCollection<EntityViewModel> ContentList    {        get { return _contentList; }    }    public CollectionViewModel()    {         _contentList = new ObservableCollection<EntityViewModel>();         _contentList.CollectionChanged += ContentCollectionChanged;    }    public void ContentCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)    {        //This will get called when the collection is changed    }}

好的,那是今天我被MSDN文档写错了。在我给您的链接中,它说:

在添加,删除,更改,移动项目或刷新整个列表时发生。

但是更改项目时实际上 不会 触发。我猜您将需要一个更强力的方法:

public class CollectionViewModel : ViewModelbase{   public ObservableCollection<EntityViewModel> ContentList    {        get { return _contentList; }    }    public CollectionViewModel()    {         _contentList = new ObservableCollection<EntityViewModel>();         _contentList.CollectionChanged += ContentCollectionChanged;    }    public void ContentCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)    {        if (e.Action == NotifyCollectionChangedAction.Remove)        { foreach(EntityViewModel item in e.OldItems) {     //Removed items     item.PropertyChanged -= EntityViewModelPropertyChanged; }        }        else if (e.Action == NotifyCollectionChangedAction.Add)        { foreach(EntityViewModel item in e.NewItems) {     //Added items     item.PropertyChanged += EntityViewModelPropertyChanged; }  }}    public void EntityViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)    {        //This will get called when the property of an object inside the collection changes    }}

如果您非常需要此功能,则可能需要将自己的子类化

ObservableCollection
CollectionChanged
当成员
PropertyChanged
自动触发事件时触发事件的子类(如文档中应该说的那样)。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/568600.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号