gpt4 book ai didi

c# - List INotifyPropertyChanged 事件

转载 作者:IT王子 更新时间:2023-10-29 04:32:51 28 4
gpt4 key购买 nike

我有一个简单的类,它有一个字符串属性和一个列表属性,我实现了 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");
}
}
}
}

最佳答案

你应该使用 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");
}

...

}

关于c# - List<string> INotifyPropertyChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8470101/

28 4 0
文章推荐: c# - 如何不将面板控件呈现为
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com