WPF binding update notification for nested property when parent property changes


   

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.

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

2 Answers

up vote 6 down vote accepted

"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):

  1. Bind to the car, not the sub property, and create a data template that displays what you want out of it.
  2. 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!

share improve this answer
 
 
Thanks Greg! I know about data templates and should not have any problems with them. I had the same issue some time ago and I remember that I found something in the past that sounds like "deep looking bindings", but I guess it was only the VeryObservableCollection I found here - which does not help with this issue. I hoped that something similiar would be existing. But the data template idea is perfect :) –   Felix C  Aug 14 '13 at 14:12
 
Sorry, what? WPF doesn't listen to changes along the path? I wish I knew this before I started using chained paths everywhere. Hey, stop. It works! ;) –   Eugene Strizhok  Jan 10 '14 at 15:14

I'm not an WPF expert, but I think it's because you've chosen the wrong path.

<TextBlock Text="{Binding Path=MyCarProperty, Value=Manufacturer}" />

update:

<TextBlock Text="{Binding Source=MyCarProperty, Path=Manufacturer}" />
share improve this answer
 
 
According to this there is no value attribute. –   Felix C  Aug 14 '13 at 10:49
 
By the way: The Binding is working, just value updates are not propagated. –   Felix C  Aug 14 '13 at 10:57
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值