相关疑难解决方法(0)

ObservableCollection没有注意到它中的Item何时发生变化(即使使用INotifyPropertyChanged)

有谁知道为什么这段代码不起作用:

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"); }
    }
}
Run Code Online (Sandbox Code Playgroud)

ViewModelBase包含所有东西RaisePropertyChanged等等,它除了这个问题以外的所有其他工作..

c# observablecollection inotifypropertychanged

158
推荐指数
10
解决办法
16万
查看次数

项目更改时通知ObservableCollection

我在这个链接上找到了

ObservableCollection没有注意到它中的Item何时发生变化(即使使用INotifyPropertyChanged)

一些通知Observablecollection项目已更改的技术.这个链接中的TrulyObservableCollection似乎正是我正在寻找的.

public class TrulyObservableCollection<T> : ObservableCollection<T>
where T : INotifyPropertyChanged
{
    public TrulyObservableCollection()
    : base()
    {
        CollectionChanged += new NotifyCollectionChangedEventHandler(TrulyObservableCollection_CollectionChanged);
    }

    void TrulyObservableCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null)
        {
            foreach (Object item in e.NewItems)
            {
                (item as INotifyPropertyChanged).PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }
        if (e.OldItems != null)
        {
            foreach (Object item in e.OldItems)
            {
                (item as INotifyPropertyChanged).PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }
    }

    void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        NotifyCollectionChangedEventArgs a = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
        OnCollectionChanged(a); …
Run Code Online (Sandbox Code Playgroud)

c# collections wpf observablecollection inotifypropertychanged

39
推荐指数
3
解决办法
12万
查看次数