使用Fody.PropertyChanged,如何使用BindingList <t>的属性?</t>

时间:2014-04-23 13:08:29

标签: c# bindinglist fody fody-propertychanged

我使用Fody.PropertyChanged编织有以下类:

[ImplementPropertyChanged]
public class OtherClass : INotifyPropertyChanged
{
    #pragma warning disable 67
    public event PropertyChangedEventHandler PropertyChanged;
    #pragma warning restore 67

    public int SomeValue { get; set; }
}

[ImplementPropertyChanged]
public class MyClass : INotifyPropertyChanged
{
    #pragma warning disable 67
    public event PropertyChangedEventHandler PropertyChanged;
    #pragma warning restore 67

    public string SomeText { get; set; }
    public BindingList<OtherClass> Others { get; private set; }

    public MyClass ()
    {
        Others = new BindingList<OtherClass>();
    }
}

从使用 MyClass 的课程中,我没有收到 PropertyChanged 事件。

这里有什么问题?

1 个答案:

答案 0 :(得分:3)

尝试添加BindingList类的ListChanged事件以引发PropertyChanged事件:

public MyClass() {
  this.Others = new BindingList<OtherClass>();
  this.Others.ListChanged += Others_ListChanged;
}

void Others_ListChanged(object sender, ListChangedEventArgs e) {
  if (this.PropertyChanged != null) {
    this.PropertyChanged(this, new PropertyChangedEventArgs("Others"));
  }
}

我的OtherClass不必实现INotifyPropertyChanged事件:

[ImplementPropertyChanged]
public class OtherClass {
  public int ID { get; set; }
}