In my ViewModel, I have a class “A” with a child property “B” that is also a custom class. Both classes implement INotifyPropertyChanged and B’s PropertyChanged event is hooked up to fire A’s PropertyChanged event (with the correct property name of “B”).
I also have a DependencyProperty “DPB” on my ViewModel that is bound to B in code with a very simple binding (new Binding(“A.B”)).
Now I have three TextBoxes in my view:
- 1 bound to A.B.C (a property of B)
- 1 bound directly to A.B
- 1 bound to DPB
On first run, both A.B and the DPB textboxes show the correct value. But when I change the A.B.C textbox, only the A.B textBox is updated – the DPB textBox is not updated.
I have debugged through all of the PropertyChanged notifying code and they are all hit with the correct values passed.
The problem seems to be that the DependencyProperty (or it’s binding) is not being updated when the PropertyChanged event is fired. Can anyone tell me why or how to change this behaviour?
thank you.
I have bad news for you.
Inside
DependencyObject.SetValuethe check is located, which verify if new value equals to old value. So if you are binded toA.B, and changing ofA.B.Cproduces PropertyChanged event forA.B,Bindingmechanizm will handle this event and even callDependencyObject.SetValue. But then (due to equality of old and newA.Bvalues) no changes will be applied to DP.In order to achieve correct DP fireing, you should create new instance of A.B, that concludes in great headache.
UPDATED
You could use Freezable object, which supports notification that it has changed when its property is changed. DependencyObject works with Freezables correctly, so next example does what you need.
Model classes:
Xaml:
Code behind: