WPF binding update notification for nested property when parent property changes

binding, c#, mvvm, wpf

Solution

"The class Car is not implementing INotifyPropertyChanged. But I'm not changing the property Manufacturer, I change the MyCarProperty property, and so I expect that the OnNotifyPropertyChanged event will trigger the value update?"

Nope, it won't trigger the value update a level down. Bindings don't listen to property changes for an entire path, they listen only to the object that they're bound to.

I see a couple options off the top of my head (in order of my preference when I run into this):

- Bind to the car, not the sub property, and create a data template that displays what you want out of it.

- Manually kick the binding by calling UpdateTarget on it's `BindingExpression` when you need to.

I know it looks like there's a lot more to learn on the data template route, but I promise you that data templates will prove vastly more powerful, scalable, maintainable, and useful than manually kicking bindings as you work more in WPF. (Also, once you understand them, I think they're actually less work than manually kicking bindings).

Good luck!

Problem

I have a ViewModel with a complex property type and want to bind my view to a nested property of this object. My ViewModel is implementing `INotifyPropertyChanged` (or do be extact `BaseViewModel` is implementing it). The class of the parent property is not implementing `INotifyPropertyChanged`. The class Car is not implementing `INotifyPropertyChanged`. But I'm not changing the property `Manufacturer`, I change the `MyCarProperty` property, and so I expect that the `OnNotifyPropertyChanged` event will trigger the value update? When I'm updating the value of the parent property, the nested property is not updating. Can you tell me how can I implement this functionality? ViewModel ``` public class ViewModel : BaseViewModel { private Car _myCarProperty; public Car MyCarProperty { get { return _myCarProperty; } set { if (value == _myCarProperty) return; _myCarProperty = value; OnPropertyChanged(); } } } ``` Binding in the View ``` <TextBlock Text="{Binding Path=MyCarProperty.Manufacturer}" /> ``` When I change the value of `MyCarProperty` the View does not update. Thanks for any help! Edit: OnPropertyChanged() implementation ``` #region INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChangedInvocator] protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } #endregion INotifyPropertyChanged ```

Original source

Related problems