使用INotifyPropertyChanged进行SortedSet

Svi*_*ack 3 c# design-patterns event-handling

我有这样的事情:

public class CPerson: INotifyPropertyChanged
public class CPeople: SortedSet<CPerson>
public class CMain
{
    private CPeople _people;
}
Run Code Online (Sandbox Code Playgroud)

我想知道CMain,如果有什么东西在改变CPeople,新的人加入或删除或东西是在一些改变CPersonCPeople,我已经实现INotifyPropertyChangedCPerson,但我没有任何高招有什么网络接口实现CPeople类以及如何好办法脱身PropertyChanged事件过CPeopleCMain.

谁能帮我?问候.

jer*_*enh 6

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