Tutorial :How do I get PropertyChanged events to bubble up?



Question:

I have a hierarchy of objects (objects A, B), each of which implements INotifyPropertyChanged such that...

A has a member of type B, B has a member of C, C is of type bool

When C changes, its PropertyChanged event gets fired, however this does not fire B's property changed event. And therefore A does not react to B's change.

Is there a good way to bubble this up?


Solution:1

This is great - I have to spend some more time looking at it. Thank you.

I found a different solution but it's a bit of a hack. I just set my binding path property to the nested type. Using my example above, in my xaml (which has a DataContext of object A) I set my binding as...

{Binding Path=B.C}

Bubbling the event up is definitely more elegant.


Solution:2

This is a fairly simplistic solution but can't you just subscribe to the PropertyChanged event and propagate the call?

E.G.

MyContainedObject.PropertyChanged += PropertyChangedHandler;  

and then in your handler:

private void PropertyChangedHandler(object sender, PropertyChangedEventArgs e)  {      this.OnPropertyChanged(e.PropertyName);  }  

This works great when your objects have properties with the same name. If you have properties with different names you will have to do a bit more work to convert between the property values.

private void PropertyChangedHandler(object sender, PropertyChangedEventArgs e)  {      switch(e.PropertyName)             {                case "Property1":             this.OnPropertyChanged("ADifferentProperty1");          break;                         case "Property2":                      this.OnPropertyChanged("ADifferentProperty2");          break;                          default:                      this.OnPropertyChanged(e.PropertyName);                     break;           }  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »