drowned
drowned

Reputation: 550

Window doesn't update when ViewModel is changed

I have a WPF application using MVVM; when I change the ViewModel in my main window ViewModel class, the new user control is not displayed in the window... the original one remains. The ViewModel looks like this:

public class MainWindowViewModel : ViewModelBase {

    public ViewModelBase Workspace;

    public MainWindowViewModel()
    {
        var w = new CustomerDetailsViewModel();

        SetActiveWorkspace(w);
    }

    void NavigationService_ViewChanged(object sender, ViewChangedEventArgs e)
    {
        SetActiveWorkspace(e.View);
    }

    void SetActiveWorkspace(ViewModelBase workspace)
    {
        Workspace = workspace;
    }
}

My XAML looks like this: < ContentControl Content="{Binding Path=Workspaces}" >

The navigation service ViewChanged event is firing, and the SetActiveWorkspace method is being called with the correct view in the argument. However, after that, the view is not reloaded. What am I missing here?

Upvotes: 1

Views: 2012

Answers (1)

Rachel
Rachel

Reputation: 132618

Your Workspace property is not raising the PropertyChanged event. It should look like this:

private ViewModelBase _workspace;

public ViewModelBase Workspace
{
    get { return _workspace; }
    set 
    {
        if (value != _workspace)
        {
            _workspace = value;

            // This raises the PropertyChanged event to let the UI know to update
            OnPropertyChanged("WorkSpace");
        }
    }
}

Make sure your ViewModelBase implements INotifyPropertyChanged

Upvotes: 3

Related Questions