1

我有以下使用 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事件。

这里有什么问题?

4

1 回答 1

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; }
}
于 2014-04-23T14:13:26.557 回答