List<string> INotifyPropertyChanged event

瘦欲@ 提交于 2019-12-18 12:48:50

问题


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");
            }
        }
    }
}

回答1:


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");
    }

    ...

}



回答2:


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.




回答3:


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.




回答4:


You should have a look at ObservableCollection



来源:https://stackoverflow.com/questions/8470101/liststring-inotifypropertychanged-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!