列表<字符串>INotifyPropertyChanged 事件字符串>
我有一个带有字符串属性和 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使用
ObservableCollection
而不是List
,因为与List
不同,ObservableCollection
将通知依赖项当其内容改变时。在您的情况下,我会将
_includeFolders
设为只读 - 您始终可以使用该集合的一个实例。You should use
ObservableCollection<string>
instead ofList<string>
, because unlikeList
,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.使 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 anObservableCollection
.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.您的列表不会自动为您触发 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 anObservableCollection<T>
, which will update automatically when items are added or removed.您应该查看 ObservableCollection
You should have a look at ObservableCollection