17

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)

Geoff
  • 1,634
  • 16
  • 25

4 Answers4

9

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
  • 14,520
  • 6
  • 56
  • 80
  • 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 Oct 14 '10 at 13:34
  • 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 Oct 14 '10 at 13:46
3

Here's a SO thread containing a solution on how to bubble up these notifications: When nesting properties that implement INotifyPropertyChanged must the parent object propogate changes?

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.

Community
  • 1
  • 1
Ben Gribaudo
  • 5,057
  • 1
  • 40
  • 75
  • Ben, did you mean to link back to this question again? – Andre Luus Oct 14 '10 at 13:24
  • 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 Oct 14 '10 at 13:25
1

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
  • 12,481
  • 7
  • 30
  • 32
  • 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 Oct 14 '10 at 12:59
  • 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 Oct 14 '10 at 13:03
0

Check out PropertyChangedPropagator, it can handle dependencies on nested view models' properties including dynamically changeable nested view models: http://www.codeproject.com/Articles/775831/INotifyPropertyChanged-propagator

KolA
  • 707
  • 7
  • 15