Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SortedSet with INotifyPropertyChanged

I have something like this:

public class CPerson: INotifyPropertyChanged
public class CPeople: SortedSet<CPerson>
public class CMain
{
    private CPeople _people;
}

I want to know in CMain if something was changed in CPeople, new person was added or deleted or something was changed in some CPerson in CPeople, i have implemented INotifyPropertyChanged on CPerson but i don't have any brilliant idea what interface implement on CPeople class and how in good way get out PropertyChanged event over CPeople to CMain.

Can anyone help me? Greetings.

like image 857
Svisstack Avatar asked Feb 25 '26 18:02

Svisstack


1 Answers

I would use ObservableCollection<Person>. If you really need a SortedSet, you can also implement the INotifyCollectionChanged and INotifyPropertyChanged interfaces yourself.

One way you could go forward could be to create your collection class wrapped around SortedSet, like so:

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;
}
like image 180
jeroenh Avatar answered Feb 28 '26 06:02

jeroenh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!