Using Fody.PropertyChanged, how to use with a property of BindingList<T>?

St&#233;cy picture Stécy · Apr 23, 2014 · Viewed 10.8k times · Source

I have the following classes using Fody.PropertyChanged weaving:

[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>();
    }
}

From a class consuming MyClass, I do not receive a PropertyChanged event.

What is wrong here?

Answer

LarsTech picture LarsTech · Apr 23, 2014

Try adding the ListChanged event of the BindingList class to raise the PropertyChanged event:

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"));
  }
}

My OtherClass did not have to implement the INotifyPropertyChanged event:

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