为了账号安全,请及时绑定邮箱和手机立即绑定

未注意到它中的项何时更改(即使使用INotifyPropertyChanged)

未注意到它中的项何时更改(即使使用INotifyPropertyChanged)

C#
慕妹3242003 2019-06-19 10:47:39
未注意到它中的项何时更改(即使使用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"); }     }}ViewModelBase把所有东西都装上RaisePropertyChanged等等,它适用于除这个问题以外的其他一切。
查看完整描述

3 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

当您更改集合中的值时,ContentList的set方法将不会被调用,而是应该查找收藏变化事件触发。

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事件自动发生(就像它在文档中说的那样.)


查看完整回答
反对 回复 2019-06-19
?
慕田峪4524236

TA贡献1875条经验 获得超5个赞

这使用了上述想法,但使其成为派生的“更敏感”的集合:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ComponentModel;
using System.Collections.ObjectModel;using System.Collections.Specialized;using System.Collections;namespace somethingelse{
    public class ObservableCollectionEx<T> : ObservableCollection<T> where T : INotifyPropertyChanged
    {
        // this collection also reacts to changes in its components' properties

        public ObservableCollectionEx() : base()
        {
            this.CollectionChanged +=new System.Collections.Specialized.NotifyCollectionChangedEventHandler
            (ObservableCollectionEx_CollectionChanged);
        }

        void ObservableCollectionEx_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                foreach(T item in e.OldItems)
                {
                    //Removed items
                    item.PropertyChanged -= EntityViewModelPropertyChanged;
                }
            }
            else if (e.Action == NotifyCollectionChangedAction.Add)
            {
                foreach(T 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 - note you must make it a 'reset' 
            - dunno why
            NotifyCollectionChangedEventArgs args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
            OnCollectionChanged(args);
        }
    }}


查看完整回答
反对 回复 2019-06-19
  • 3 回答
  • 0 关注
  • 782 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信