可观察集合集合改变的问题

发布于 2024-10-11 23:00:22 字数 1681 浏览 5 评论 0原文

我正在学习可观察集合,因此我编写了一个小程序来测试我的进度。 我有一个可观察集合类,我从列表中提供初始值并将可观察集合绑定到数据网格。它工作得很好,但是当我使用 myListOfPlayers.myList.Clear() 清除列表时,数据网格不会清除。我认为 INotifyPropertyChanged 属性可以处理这个问题。我做错了什么?

public class PlayerList : ObservableCollection<PlayerName> //observable collection class
    {
        public PlayerList()
            : base()
        {
            Clear();

            foreach (var p in myListOfPlayers.myList.OrderBy(x => x.Score))
            {
                Add(p);
            }
        }

        public PlayerList(List<PlayerName> list)
            : base(list)
        {
            Clear();

            foreach (var p in list)
            {
                Add(p);
            }
        }
    }

我在 PlayerName 类中实现 INotifyPropertyChanged:

public class PlayerName : INotifyPropertyChanged
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }

        private int _score;

        public int Score
        {
            get { return _score; }
            set
            {
                _score = value;
                OnPropertyChanged("Score");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

I am learning about observable collections, so I wrote a small program to test my progress.
I have a observable collection class that I supply initial values from a list and bind the observablecollection to a Datagrid. It works great, but when I clear the list using myListOfPlayers.myList.Clear(), the Datagrid does not clear. I thought that the INotifyPropertyChanged property would handle that. What am I doing wrong?

public class PlayerList : ObservableCollection<PlayerName> //observable collection class
    {
        public PlayerList()
            : base()
        {
            Clear();

            foreach (var p in myListOfPlayers.myList.OrderBy(x => x.Score))
            {
                Add(p);
            }
        }

        public PlayerList(List<PlayerName> list)
            : base(list)
        {
            Clear();

            foreach (var p in list)
            {
                Add(p);
            }
        }
    }

I implement INotifyPropertyChanged in the PlayerName class:

public class PlayerName : INotifyPropertyChanged
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }

        private int _score;

        public int Score
        {
            get { return _score; }
            set
            {
                _score = value;
                OnPropertyChanged("Score");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

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

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

发布评论

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

评论(1

风柔一江水 2024-10-18 23:00:22

如果您清除 myListOfPlayers.myList,则 PlayerList 不应被清除...这两个列表之间没有关系:您刚刚使用了 myListOfPlayers.myList来初始化PlayerList的内容,但它们是两个不同的列表。您对其中一个所做的操作不会影响另一个

If you clear myListOfPlayers.myList, the PlayerList is not supposed to be cleared... There is no relation between these 2 lists: you just used myListOfPlayers.myList to initialize the content of PlayerList, but they are two different lists. What you do on one of them doesn't affect the other

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