如何在自定义集合中查看集合对象的更改

时间:2013-07-04 11:39:31

标签: c# sorting generics collections inotifypropertychanged

我正在创建一个基于列表的自定义集合,其目的是成为Observable,Prevent Duplicates,Sorted等。

我有一个像这样的添加方法:

    public class SortedObservableCollection<T> : List<T>, INotifyPropertyChanged, INotifyCollectionChanged
    {
        public void Add(T item)
        {
            base.Add(item);
            base.Sort();
            this.OnPropertyChanged("Count");
            this.OnPropertyChanged("Item[]");
            this.OnCollectionChanged(NotifyCollectionChangedAction.Add, item);
        }
}

但不知何故,当在集合中直接更改对象时,我需要自动执行,或者通知集合,以便它可以再次进行排序。

喜欢:

SortedObservableCollection<IRCUser> users = new SortedObservableCollection<IRCUser>();
users.Add(new IRCUser { Nick = "User1", Status = IRCUserStatus.Unknown });
users.Add(new IRCUser { Nick = "User2", Status = IRCUserStatus.Unknown });
users.Add(new IRCUser { Nick = "User3", Status = IRCUserStatus.Unknown });


users.Single(x => x.Nick == "User3").Nick = "User11";

User3停留在集合的底部,请注意我使用自己的IComparable实现,而不仅仅是按字母顺序排列。

我需要这个集合才能抓住这个变化并通知我,这样我才能做点什么。

我知道我必须在对象上实现INotifyPropertyChanged,但我不确定的是集合将如何捕获它,我能看到的唯一方法是在我使用的add方法上:

item.PropertyChanged += (o,e) { ..check then... base.Sort(); };

但是,如果我有10,000个用户,这是要走的路吗?

2 个答案:

答案 0 :(得分:0)

项目类必须实现INotifyPropertyChanged 那你有两个选择: 保存子类(item)中的列表(父)和项属性更改,您调用某个方法

或者当您向列表中添加项目时,添加其NotifyPropertyChanged事件。我会那样走。

列表:

public listclass()
{
  this.ListChanged += new ListChangedEventHandler(listclass_ListChanged);
}    

void listclass_ListChanged(object sender, ListChangedEventArgs e)
{
  if (e.ListChangedType == ListChangedType.ItemAdded)
  {
    item item = this[e.NewIndex];
    item.PropertyChanged += new PropertyChangedEventHandler(Item_PropertyChanged);
  }
}

void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
   if (e.PropertyName == "propertyname")
   {

   }     
}

item实现了INotifyPropertyChanged:

public item()
{      
  this.PropertyChanged += new PropertyChangedEventHandler(AnyPropertyChanged); 
}

private void AnyPropertyChanged(object sender, PropertyChangedEventArgs e)
{
  // example how to use
  if (e.PropertyName == "anyproperyname")
  {

  }
}

public event PropertyChangedEventHandler PropertyChanged;
public void InvokePropertyChanged(PropertyChangedEventArgs e)
{
  PropertyChangedEventHandler handler = PropertyChanged;
  if (handler != null) handler(this, e);
}

private Boolean bTheProperty = true;
public Boolean TheProperty
{
   get { return bTheProperty ; }
   set 
   { 
     if (bTheProperty != value) 
     { 
       bTheProperty = value; 
       InvokePropertyChanged(new PropertyChangedEventArgs("TheProperty")); 
     }           
   }
}

在某些列表中,您必须将此属性设置为true,因此它会触发listchanged事件:

this.RaiseListChangedEvents = true;

如果您不想使用属性更改事件,您可以采用这种方式:

private list oParent

public item(list parent)
{
  this.oParent = parent;
}

private string sName;
public string Name
{
   get { return sName; }
   set 
   { 
     if (sName!= value) 
     { 
       sName= value; 
       if(this.parent != null)
       {  
         this.parent.IsSorted = false; 
       }
     }           
   }
}

然后你可以在列表类中使用一个计时器并检查isSorted是否为false,或者在list.IsSorted属性上调用propertychanged。

答案 1 :(得分:0)

我认为似乎解决方案是为集合中的每个对象添加一个侦听器,并在删除该项时将其删除。我可以像这样实现:

public class SortedObservableList<T> : List<T>, INotifyPropertyChanged, INotifyCollectionChanged where T : INotifyPropertyChanged, IComparable<T>
{
    public void Add(T item)
    {
        base.Add(item);
        base.Sort();
        this.OnPropertyChanged("Count");
        this.OnPropertyChanged("Item[]");
        this.OnCollectionChanged(NotifyCollectionChangedAction.Add, item);
        item.PropertyChanged += InnerObjectChanged;
    }

    private void InnerObjectChanged(object sender, PropertyChangedEventArgs e)
    {
        base.Sort();
    }

    public bool Remove(T item)
    {
        item.PropertyChanged -= InnerObjectChanged;
        bool result = base.Remove(item);
        this.OnPropertyChanged("Count");
        this.OnPropertyChanged("Item[]");
        this.OnCollectionChanged(NotifyCollectionChangedAction.Remove, item);
        return result;
    }

    protected virtual void OnPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    protected virtual void OnCollectionChanged(NotifyCollectionChangedAction action, object item)
    {
        if (this.CollectionChanged != null)
        {
            this.CollectionChanged(this, new NotifyCollectionChangedEventArgs(action, item));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public event NotifyCollectionChangedEventHandler CollectionChanged;
}

<强> IRCUser

    public class IRCUser : IComparable<IRCUser>, INotifyPropertyChanged
    {
        private string _nick;
        public string Nick
        {
            get
            {
                return _nick;
            }
            set
            {
                if (value != _nick)
                {
                    ...
                    OnPropertyChanged();
                }
            }
        }

        public int CompareTo(IRCUser other)
        {
...
        }


        private void OnPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

我刚试过这个,似乎有效。