C# 已更改InotifyProperty的已分类数据集
我有这样的想法:C# 已更改InotifyProperty的已分类数据集,c#,design-patterns,event-handling,C#,Design Patterns,Event Handling,我有这样的想法: public class CPerson: INotifyPropertyChanged public class CPeople: SortedSet<CPerson> public class CMain { private CPeople _people; } 公共类CPerson:INotifyPropertyChanged 公共类CPeople:SortedSet 公共类CMain { 私人公司员工; } 我想知道在CMain中,是否有人在CP
public class CPerson: INotifyPropertyChanged
public class CPeople: SortedSet<CPerson>
public class CMain
{
private CPeople _people;
}
公共类CPerson:INotifyPropertyChanged
公共类CPeople:SortedSet
公共类CMain
{
私人公司员工;
}
我想知道在CMain
中,是否有人在CPeople
中更改了某些内容,是否有人在CPeople
中添加或删除了新的人员,或者有人在CPeople
中更改了某些内容,我已经在CPerson
上实现了INotifyPropertyChanged
,但是我不知道CPeople
类上实现了什么接口,以及如何以良好的方式将PropertyChanged
事件转移到CPeople
到CMain
有人能帮我吗?
问候。创建您自己的界面,
IPeopleChanged
等,事件包括PersonAdded(Person p)
,PersonRemoved(Person p)
,以及类似PersonPropertyChanged(Person p,propertychangedeventarg)
,使用集合中的add方法将集合中添加到集合中的项目的属性更改事件订阅到集合中,并将这些事件放入气泡中,然后在CMain中订阅集合中的事件
总之,您的集合现在有3个事件。一个在添加项时触发,一个在删除项时触发,另一个在项发生更改时沿链传递PropertyChanged事件。如果必须使用SortedSet,您可以创建一个实现INotifyCollectionChanged的子类(从SortedSet开始)。或者,如果您没有绑定到SortedSet,请使用ObservableCollection。我会使用。如果确实需要SortedSet,还可以自己实现INotifyCollectionChanged和INotifyPropertyChanged接口 一种方法是围绕SortedSet创建collection类,如下所示:
public class ObservableSortedSet<T> : ICollection<T>,
INotifyCollectionChanged,
INotifyPropertyChanged
{
readonly SortedSet<T> _innerCollection = new SortedSet<T>();
public IEnumerator<T> GetEnumerator()
{
return _innerCollection.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(T item)
{
_innerCollection.Add(item);
// TODO, notify collection change
}
public void Clear()
{
_innerCollection.Clear();
// TODO, notify collection change
}
public bool Contains(T item)
{
return _innerCollection.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
_innerCollection.CopyTo(array, arrayIndex);
}
public bool Remove(T item)
{
_innerCollection.Remove(item);
// TODO, notify collection change
}
public int Count
{
get { return _innerCollection.Count; }
}
public bool IsReadOnly
{
get { return ((ICollection<T>)_innerCollection).IsReadOnly; }
}
// TODO: possibly add some specific methods, if needed
public event NotifyCollectionChangedEventHandler CollectionChanged;
public event PropertyChangedEventHandler PropertyChanged;
}
公共类ObserviesOrtedSet:ICollection,
INotifyCollectionChanged,
InotifyProperty已更改
{
只读分拣集_innerCollection=新分拣集();
公共IEnumerator GetEnumerator()
{
返回_innerCollection.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
公共作废新增(T项)
{
_innerCollection.Add(项);
//TODO,通知集合更改
}
公共空间清除()
{
_Clear();
//TODO,通知集合更改
}
公共布尔包含(T项)
{
return\u innerCollection.Contains(项目);
}
public void CopyTo(T[]数组,int arrayIndex)
{
_CopyTo(数组,arrayIndex);
}
公共布尔删除(T项)
{
_innerCollection.Remove(项目);
//TODO,通知集合更改
}
公共整数计数
{
获取{return\u innerCollection.Count;}
}
公共图书馆是只读的
{
获取{return((ICollection)\u innerCollection.IsReadOnly;}
}
//TODO:如果需要,可能会添加一些特定的方法
公共事件通知CollectionChangedEventHandler CollectionChanged;
公共事件属性更改事件处理程序属性更改;
}