实现 INotifyCollectionChanged 接口

发布于 2024-10-08 22:35:06 字数 1491 浏览 9 评论 0原文

我需要实现一个具有特殊功能的集合。另外,我想将此集合绑定到 ListView,因此我最终得到了下一个代码(我在论坛中省略了一些方法以使其更短):

public class myCollection<T> : INotifyCollectionChanged
{
    private Collection<T> collection = new Collection<T>();
    public event NotifyCollectionChangedEventHandler CollectionChanged;

    public void Add(T item)
    {
        collection.Insert(collection.Count, item);
        OnCollectionChange(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
    }

    protected virtual void OnCollectionChange(NotifyCollectionChangedEventArgs e)
    {
        if (CollectionChanged != null)
            CollectionChanged(this, e);
    }
}

我想用一个简单的数据类来测试它:

public class Person
{
    public string GivenName { get; set; }
    public string SurName { get; set; }
}

所以我创建了myCollection 类的实例如下:

myCollection<Person> _PersonCollection = new myCollection<Person>();
public myCollection<Person> PersonCollection
{ get { return _PersonCollection; } }

问题是,尽管我实现了 INotifyCollectionChanged 接口,但集合更新时 ListView 不会更新。

我知道我的绑定很好(在 XAML 中),因为当我使用 ObservableCollecion 类而不是 myCollecion 类时,如下所示:

 ObservableCollection<Person> _PersonCollection = new ObservableCollection<Person>();
    public ObservableCollection<Person> PersonCollection
    { get { return _PersonCollection; } }

ListView 更新

问题是什么?

I need to implement a collection with special capabilities. In addition, I want to bind this collection to a ListView, Therefore I ended up with the next code (I omitted some methods to make it shorter here in the forum):

public class myCollection<T> : INotifyCollectionChanged
{
    private Collection<T> collection = new Collection<T>();
    public event NotifyCollectionChangedEventHandler CollectionChanged;

    public void Add(T item)
    {
        collection.Insert(collection.Count, item);
        OnCollectionChange(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
    }

    protected virtual void OnCollectionChange(NotifyCollectionChangedEventArgs e)
    {
        if (CollectionChanged != null)
            CollectionChanged(this, e);
    }
}

I wanted to test it with a simple data class:

public class Person
{
    public string GivenName { get; set; }
    public string SurName { get; set; }
}

So I created an instance of myCollection class as follows:

myCollection<Person> _PersonCollection = new myCollection<Person>();
public myCollection<Person> PersonCollection
{ get { return _PersonCollection; } }

The problem is that the ListView does not update when the collection updates although I implemented the INotifyCollectionChanged interface.

I know that my binding is fine (in XAML) because when I use the ObservableCollecion class instead of myCollecion class like this:

 ObservableCollection<Person> _PersonCollection = new ObservableCollection<Person>();
    public ObservableCollection<Person> PersonCollection
    { get { return _PersonCollection; } }

the ListView updates

What is the problem?

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

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

发布评论

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

评论(1

旧人九事 2024-10-15 22:35:06

为了让您的集合被使用,您还应该实现 IEnumerable 和 IEnumerator。不过,您最好将 ObservableCollection子类化

In order for your collection to be consumed, you should implement IEnumerable and IEnumerator too. Although, you're probably better off subclassing ObservableCollection<T>

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