views:

116

answers:

3

I'm trying to figure out the best way to bubble up PropertyChanged events from nested Properties in my ModelView. Say I have my ModelView PersonModelView which has a Property PersonModelView.Address. Address in turn has a property City. When I bind to City in my view, I would do something like {Binding Address.City}.

My problem is that even if Address implements INotifyPropertyChanged, the binding will not get updated because it is handling PropertyChanged on the PersonModelView, not Address. I think I have two options: (1) change the source of the binding (or change the DataContext) to the Address property or (2) have the PersonModelView handle PropertyChanged on the Address object and refire its own PropertyChanged with something like Address.City.

How are you guys solving this? (I'm using MVVM light toolkit at the mo, but am interested in any approaches)

A: 

Does your Address object implement INotifyPropertyChanged? If not, I think that would fix the issue that you are seeing.

Edit: Sorry, just noticed that you mentioned in your post that you tried this already. Have you tried subscribing to the PropertyChanged event of the Address object in your PersonViewModel? OnChanged, you could perform a PropertyChanged on your Address object.

JSprang
Yes it does. So should the binding handle the PropertyChanged event on PersonModelView or Address if the binding is {Binding Address.City}? (and the PersonModelView is the DataContext of course)
Geoff
Could you post your code? It seems to me that the way you have it set up should work. Have you looked at your 'Output' window to make sure that it's binding correctly?
JSprang
+4  A: 

If Address implements INotifyPropertyChanged and correctly raises PropertyChanged events on its City property then the binding should notice that the property it is bound to has changed.

Jackson Pope
I Agree. I've never had to propagate events up when a field a level or two down has been bound to. Try firing `PropertyChanged` with `null` as the property name and put a breakpoint in your property setter to make sure the binding works (or check the output window).
Andre Luus
That's a real relief, it should make my code much nicer. I guess I must have made a mistake when I first tried this, I just did it again in a new solution and it works fine. Thanks for the help guys!
Geoff
A: 

Here's a SO thread containing a solution on how to bubble up these notifications: http://stackoverflow.com/questions/1336414/when-nesting-properties-that-implement-inotifypropertychanged-must-the-parent-obj

However, IIRC WPF has the intelligence to automatically monitor Address for INotifyPropertyChanged notifications when a control's binding is set to Address.City without PersonViewModel needing to re-broadcast the Address object's update notifications.

Ben Gribaudo
Ben, did you mean to link back to this question again?
Andre Luus
Erm, I think that's the wrong link ;) Right, I must have missed something. I thought that the bindings couldn't handle that, back to the code for me!
Geoff
Oops! Thanks for catching that. Link corrected.
Ben Gribaudo