Tuesday, February 8, 2011

MVVM Basic Binding - reflecting multiple targets

I have a ViewModel implementing INotifyPropertyChanged interface and have at least two properties Value and IsChanged. When I bind two Textboxes to the Value on source and one check box to IsChanged property of the source I noticed that user changes to Value does not reflect the other target controls!

This behavior is due to the fact that my ViewModel was not provided as a field in my container ViewModel and rather it was created when needed.


public PandViewModel PandViewModel
{
get
{
return listIndex < 0
?
null
:
new PandViewModel(panden[listIndex]);
}
}

Moving the PandViewModel to a local field in this container class, or even putting it into an ObservableCollection will sort out the problem:
public PandViewModel PandViewModel
{
get
{
return listIndex < 0
?
null
: pandenVM[listIndex];
}
}


See also a nice article over Binding on MSDN.