locked
Object change binding issue RRS feed

  • Question

  • Binding just confuses me!

    I set the page's DataContext to a dependency property on the code behind of type "MyObject", having populated it.

    MyObject contains a dependency property called "CurrentObject" of type "MyOtherObject".

    A grid on the page binds to a DataContext  of {Binding CurrentObject, Mode=TwoWay}.

    A TextBlock.Text in the grid binds to {Binding Name} - which is a string dependency property on MyOtherObject.

    All good, you open the page and the name is displayed.

    Now if I press a button and programmatically change CurrentObject to point to a second instance of MyOtherObject, nothing changes - I want the name to change to that of the second object (amongst other things, but lets keep this simple!).  I can see the value of CurrentObject changing from its property changed event, but the text on the page doesn't change to the new value.

    Am I just approaching this in the wrong way? Should I be using an observable collection of two objects or something?

    Thanks,


    Thanks, R

    Wednesday, November 13, 2013 1:27 PM

Answers

  • Hi, Reece

    You shoud use use ObservableCollection(Of T) instead. For TwoWay bindings, dynamic changes to the source don't automatically propagate to the target without providing some support from the source. You must implement the INotifyPropertyChanged interface on the source object so that the source can report changes through events that the binding engine listens for.ObservableCollection(Of T) already implement System.ComponentModel.INotifyPropertyChanged.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Dave SmitsMVP Thursday, November 14, 2013 7:18 AM
    • Marked as answer by Anne Jing Tuesday, November 19, 2013 2:41 AM
    Thursday, November 14, 2013 5:45 AM