c# – 使用INotifyPropertyChanged进行SortedSet
发布时间:2020-12-16 01:39:29 所属栏目:百科 来源:网络整理
导读:我有这样的事情: public class CPerson: INotifyPropertyChangedpublic class CPeople: SortedSetCPersonpublic class CMain{ private CPeople _people;} 我想在CMain中知道,如果CPeople中的某些内容发生了变化,新人被添加或删除,或者某些CPerson中的某些内
我有这样的事情:
public class CPerson: INotifyPropertyChanged public class CPeople: SortedSet<CPerson> public class CMain { private CPeople _people; } 我想在CMain中知道,如果CPeople中的某些内容发生了变化,新人被添加或删除,或者某些CPerson中的某些内容发生了变化,我已经在CPerson上实现了INotifyPropertyChanged,但我对CPeople类的接口实现没有任何好主意以及如何以良好的方式将CPCople的PropertyChanged事件发送给CMain. 谁能帮我? 解决方法
我会使用
ObservableCollection<Person> .如果你真的需要一个SortedSet,你也可以自己实现INotifyCollectionChanged和INotifyPropertyChanged接口.
你可以前进的一种方法是创建一个包裹在SortedSet中的集合类,如下所示: 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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |