2011-09-09 313 views
2
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”在编译。我有一个可观察的集合,我希望能够在可观察集合中的每个项目都被更改时访问通知,所以我不能看到我做错了什么。请有任何想法吗?不实现接口成员“System.ComponentModel.INotifyPropertyChanged.PropertyChanged”

谢谢。

回答

2

错误消息与可观察集合无关。你声明TreeManager实现了INotifyPropertyChanged,所以你必须实现接口成员。

根据documentation on INotifyPropertyChanged,为了做到这一点,您必须执行事件PropertyChanged - 正是编译器所抱怨的。

4

你都在6年代和7的

首先这个类并不需要执行INotifyPropertyChanged为您订阅事件上观察集合。

此外,如果你正在尝试(这是我如何阅读你的问题),看看集合中的项目是否已经改变,那么他们需要实现INotifyPropertyChanged在一些为什么直接或通过inherting fron ObservableObject。

其次它是PropertyChanged您需要订阅而不是集合更改。

相关问题