我有一个带有字符串属性和List属性的简单类,并且实现了INofityPropertyChanged事件,但是当我执行.Add到字符串List时,此事件未命中,因此我在Converter中显示在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");
}
}
}
}
最佳答案
您应该使用ObservableCollection<string>
而不是List<string>
,因为与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");
}
...
}