的ObservableCollection没有注意到,当它改变项(即使有INotifyPropertyChanged的) [英] ObservableCollection not noticing when Item in it changes (even with INotifyPropertyChanged)

查看:229
本文介绍了的ObservableCollection没有注意到,当它改变项(即使有INotifyPropertyChanged的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有人知道为什么会这样code不工作:

does anyone know why this code doesn't work:

public class CollectionViewModel : ViewModelBase {  
    public ObservableCollection<EntityViewModel> ContentList
    {
        get { return _contentList; }
        set 
        { 
            _contentList = value; 
            RaisePropertyChanged("ContentList"); 
            //I want to be notified here when something changes..?
            //debugger doesn't stop here when IsRowChecked is toggled
        }
     }
}

public class EntityViewModel : ViewModelBase
{

    private bool _isRowChecked;

    public bool IsRowChecked
    {
        get { return _isRowChecked; }
        set { _isRowChecked = value; RaisePropertyChanged("IsRowChecked"); }
    }
}

PS:ViewModelBase containts家居RaisePropertyChanged等,它的工作一切,除了这个问题..

PS: ViewModelBase containts everything for RaisePropertyChanged etc. and it's working for everything else except this problem..

推荐答案

当您更改集合中的一个值。ContentList的设置方法不会被调用,而不是你应该寻找出的的CollectionChanged 事件触发。

The ContentList's Set method will not get called when you change a value inside the collection, instead you should be looking out for the CollectionChanged event firing.

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文档犯错的两倍。在链接我给你这样说的:

Okay, that's twice today I've been bitten by the MSDN documentation being wrong. In the link I gave you it says:

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

Occurs when an item is added, removed, changed, moved, or the entire list is refreshed.

但它实际上的当一个项目改变不的火灾。我猜你需要再一个更暴力破解的方法:

But it actually doesn't fire when an item is changed. I guess you'll need a more bruteforce method then:

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 触发 Col​​lectionChanged 事件当一个成员会自动触发其的PropertyChanged 事件(像它说,它应该在文档中......)

If you are going to need this a lot you may want to subclass your own ObservableCollection that triggers the CollectionChanged event when a member triggers its PropertyChanged event automatically (like it says it should in the documentation...)

这篇关于的ObservableCollection没有注意到,当它改变项(即使有INotifyPropertyChanged的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆