I am creating an application that works like the ‘wizard’ component of some microsoft applications. To represent this, I have two ViewModels:
class WizardVm {
public string Name { get; set; }
public ICommand QuitCommand { get { /* ommitted */ } }
public WizardStepVm CurrentStep { get; set; }
}
class WizardStepVm {
public string StepName { get; set; }
public string StepText {get; set; }
}
In the view, WizardVm binds to a window, and WizardStepVmbinds to a content panel inside the window. I am creating the content control programatically, and adding it to the WizardView like this:
// in the "DataContextChanged" handler for the WizardView
var bn = new Binding("CurrentStep");
bn.Mode = BindingMode.OneWay;
var contentControl = new ContentControl();
contentControl.SetBinding(ContentControl.ContentProperty, bn);
WizardViewStackPanel.Children.Add(contentControl);
This renders correctly when the WizardView is initially created. However, if CurrentStep changes, the view does not update to reflect this. CurrentStep changes, but ContentControl continues to display the original WizardStepVm. Also, the old WizardStepVm continues to exist in memory, and its string properties can still be changed (from the view).
Why is this? What do I have to do so that the content control changes to reflect changes in the ViewModel that it is bound to?
(There is actually a good reason for doing this programatically. However, xaml solutions are also appreciated.)
Your class needs to implement the INotifyPropertyChanged interface to notify the UI every time
one of its properties changes: