![](/img/trans.png)
[英]WPF PropertyChanged code error: cannot implement 'System.ComponentModel.INotifyPropertyChanged
[英]Does not implement interface member 'System.ComponentModel.INotifyPropertyChanged.PropertyChanged'
namespace MimicCreation
{
public class TreeManager : INotifyPropertyChanged
{
public TreeManager() { }
public TreeManager(string title, string type, string filename)
{
this.childElementsValue.CollectionChanged += this.OnCollectionChanged;
Title = title;
Type = type;
FileName = filename;
}
public string Title { get; set; }
public string Type { get; set; }
public string FileName { get; set; }
public override string ToString()
{
return Title;
}
private ObservableCollection<TreeManager> childElementsValue = new ObservableCollection<TreeManager>();
public ObservableCollection<TreeManager> ChildElements
{
get { return childElementsValue; }
set { childElementsValue = value; }
}
public void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (TreeManager item in e.NewItems)
{
((System.ComponentModel.INotifyPropertyChanged)item).PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(OnPropertyChanged);
}
break;
}
}
public void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
}
}
}
我收到以下错误:编译时错误'MimicCreation.TreeManager'未实现接口成员'System.ComponentModel.INotifyPropertyChanged.PropertyChanged'。 我有一个可观察的集合,当可观察的集合中的每个项目都更改时,我希望能够访问通知,但看不到我做错了什么。 有什么想法吗?
谢谢。
你们都在6和7
首先,此类无需实现INotifyPropertyChanged即可让您订阅可观察集合上的事件。
另外,如果您正在尝试(这就是我阅读您的问题的方式)以查看集合中的项目是否已更改,则他们需要直接或通过继承ObservableObject来实现INotifyPropertyChanged。
其次,它是PropertyChanged,您需要订阅未更改的集合。
该错误消息与可观察的集合无关。 您声明TreeManager
实现INotifyPropertyChanged
,因此您必须实现接口成员。
根据documentation on INotifyPropertyChanged
的documentation on INotifyPropertyChanged
,为此,您必须实现PropertyChanged
事件-正是编译器抱怨的事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.