列表<字符串>INotifyPropertyChanged 事件

发布于 2024-12-20 12:00:24 字数 1336 浏览 3 评论 0原文

我有一个带有字符串属性和 List 属性的简单类,并且已实现 INofityPropertyChanged 事件,但是当我执行 .Add 到字符串列表时,不会触发此事件,因此不会触发要在 ListView 中显示的转换器。我猜想添加到列表中不会命中已更改的属性...我怎样才能以某种方式实现此属性以获得该属性已更改的事件命中???

我需要使用其他类型的集合吗?

感谢您的帮助!

namespace SVNQuickOpen.Configuration
{
    public class DatabaseRecord : INotifyPropertyChanged 
    {
        public DatabaseRecord()
        {
            IncludeFolders = new List<string>();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void Notify(string propName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
        #endregion

        private string _name;

        public string Name
        {
            get { return _name; }

            set
            {
                this._name = value;
                Notify("Name");
            }
        }

        private List<string> _includeFolders;

        public List<string> IncludeFolders
        {
            get { return _includeFolders; }

            set
            {
                this._includeFolders = value;
                Notify("IncludeFolders");
            }
        }
    }
}

I have a simple class with a string property and a List property and I have the INofityPropertyChanged event implemented, but when I do an .Add to the string List this event is not hit so my Converter to display in the ListView is not hit. I am guessing the property changed is not hit for an Add to the List....how can I implement this in a way to get that property changed event hit???

Do I need to use some other type of collection?!

Thanks for any help!

namespace SVNQuickOpen.Configuration
{
    public class DatabaseRecord : INotifyPropertyChanged 
    {
        public DatabaseRecord()
        {
            IncludeFolders = new List<string>();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void Notify(string propName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
        #endregion

        private string _name;

        public string Name
        {
            get { return _name; }

            set
            {
                this._name = value;
                Notify("Name");
            }
        }

        private List<string> _includeFolders;

        public List<string> IncludeFolders
        {
            get { return _includeFolders; }

            set
            {
                this._includeFolders = value;
                Notify("IncludeFolders");
            }
        }
    }
}

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

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

发布评论

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

评论(4

櫻之舞 2024-12-27 12:00:24

您应该使用 ObservableCollection 而不是 List,因为与 List 不同,ObservableCollection 将通知依赖项当其内容改变时。

在您的情况下,我会将 _includeFolders 设为只读 - 您始终可以使用该集合的一个实例。

public class DatabaseRecord : INotifyPropertyChanged 
{
    private readonly ObservableCollection<string> _includeFolders;

    public ObservableCollection<string> IncludeFolders
    {
        get { return _includeFolders; }
    }

    public DatabaseRecord()
    {
        _includeFolders = new ObservableCollection<string>();
        _includeFolders.CollectionChanged += IncludeFolders_CollectionChanged;
    }

    private void IncludeFolders_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        Notify("IncludeFolders");
    }

    ...

}

You should use ObservableCollection<string> instead of List<string>, because unlike List, ObservableCollection will notify dependents when its contents are changed.

And in your case I'd make _includeFolders readonly - you can always work with one instance of the collection.

public class DatabaseRecord : INotifyPropertyChanged 
{
    private readonly ObservableCollection<string> _includeFolders;

    public ObservableCollection<string> IncludeFolders
    {
        get { return _includeFolders; }
    }

    public DatabaseRecord()
    {
        _includeFolders = new ObservableCollection<string>();
        _includeFolders.CollectionChanged += IncludeFolders_CollectionChanged;
    }

    private void IncludeFolders_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        Notify("IncludeFolders");
    }

    ...

}
爱格式化 2024-12-27 12:00:24

使 WPF 的列表绑定工作的最简单方法是使用实​​现 INotifyCollectionChanged 的集合。这里要做的一个简单的事情就是用 ObservableCollection

如果您使用 ObservableCollection,那么每当您修改列表时,它都会引发 CollectionChanged 事件 - 该事件将告诉 WPF 绑定进行更新。请注意,如果您换出实际的集合对象,您将需要引发实际集合属性的 propertychanged 事件。

The easiest way to make WPF's list binding work is to use a collection that implements INotifyCollectionChanged. A simple thing to do here is to replace or adapt your list with an ObservableCollection.

If you use ObservableCollection, then whenever you modify the list, it will raise the CollectionChanged event - an event that will tell the WPF binding to update. Note that if you swap out the actual collection object, you will want to raise the propertychanged event for the actual collection property.

冬天的雪花 2024-12-27 12:00:24

您的列表不会自动为您触发 NotifyPropertyChanged 事件。

公开 ItemsSource 属性的 WPF 控件设计为绑定到 ObservableCollection,该控件在添加或删除项目时自动更新。

Your List is not going to fire the NotifyPropertyChanged event automatically for you.

WPF controls that expose an ItemsSource property are designed to be bound to an ObservableCollection<T>, which will update automatically when items are added or removed.

层林尽染 2024-12-27 12:00:24

您应该查看 ObservableCollection

You should have a look at ObservableCollection

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