带 INotifyPropertyChanged 的​​ SortedSet

发布于 2024-10-04 07:05:53 字数 545 浏览 2 评论 0原文

我有这样的事情:

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

我想知道在 CMain 中,CPeople 中是否发生了某些更改,添加或删除了新人员,或者某些 CPerson< 中发生了某些更改/code> 在 CPeople 中,我已经在 CPerson 上实现了 INotifyPropertyChanged 但我不知道在 CPeople 上实现了什么接口 类以及如何通过 CPeopleCMain 发出 PropertyChanged 事件。

谁能帮助我吗? 问候。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慕烟庭风 2024-10-11 07:05:53

我会使用 ObservableCollection。如果您确实需要 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;
}

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;
}
乄_柒ぐ汐 2024-10-11 07:05:53

如果您必须使用 SortedSet<>也许您可以创建一个实现 INotifyCollectionChanged 的​​后代类(来自 SortedSet<>)。或者,如果您不依赖于 SortedSet<>使用 ObservableCollection<>反而。

If you have to use SortedSet<> maybe you could create a descendant class (from SortedSet<>) that implements INotifyCollectionChanged. Or if you are not tied to SortedSet<> use ObservableCollection<> instead.

掩耳倾听 2024-10-11 07:05:53

创建您自己的界面,IPeopleChanged 等,其中包含诸如 PersonAdded(Person p)PersonRemoved(Person p) 等事件以及诸如 PersonPropertyChanged(Person p,PropertyChangedEventArgs arg),使用集合中的 add 方法为集合订阅添加到集合中的项目的属性更改事件,并将这些事件一起冒泡,然后订阅集合中的事件在 CMain.

总而言之,您的集合现在有 3 个事件。一个在添加项目时触发,一个在删除项目时触发,一个在项目更改时沿着链传递 PropertyChanged 事件。

Create your own interface, IPeopleChanged etc, with events like PersonAdded(Person p),PersonRemoved(Person p),and something like PersonPropertyChanged(Person p,PropertyChangedEventArgs arg), use the add method in your collection to subscribe your collection to the property change events from your items added to the collection, and bubble these along, and subscribe to the events in your collection in CMain.

So to sum up, your collection now has 3 events.one which fires when an item added,one when an item removed, and one that passes the PropertyChanged event along the chain when an item has changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文