1

I have a Method in MainWindowViewModel.cs to navigate the view by passing viewname as a parameter.

Navigation Method(By Relay Command)

public void onNav(string destination)
{
    switch (destination)
    {
        case "study":
            CurrentViewModel = new StudyViewModel();
            break;
        case "capture":
            CurrentViewModel = new ImageCaptureViewModel();
            break;
        case "register":
            CurrentViewModel = new RegisterViewModel();
            break;
        case "editing":
            CurrentViewModel = new ImageEditingViewModel();
            break;
        default:
            CurrentViewModel = new ImageCaptureViewModel();
            break;
        }
    }
}

When I am calling from RegisterViewModel this method is triggered and CurrentViewModel values updated. But the corresponding View is not showing in the Window.

var mainModel = new MainWindowViewModel();
mainModel.OnNav("study");

It is updating ViewModel but not updating View. How to fix it.

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
BALA G
  • 75
  • 10
  • You're creating a new instane of MainWindowViewModel and call the OnNav-Method there. I guess that this is not the same instance as your MainWindowView is bound to – Tomtom Nov 17 '16 at 10:03
  • @Tomtom I dont no how to do this. What is the proper way to do that? – BALA G Nov 17 '16 at 10:15

2 Answers2

0

WPF has a little difference to UWP. You don't update the existed view, you close the current view and open a new windows. In your case: Consider the StudyViewModel is the name of the ViewModel and Study is the name of the View.

public void onNav(string destination)
{
    switch (destination)
    {
        case "study":
            Study window = new Study();
            break;
        ...
        default:
            ImageCapture window = new ImageCapture();
            break;
    }

    //Show the window
    window.Show();
}

This is not relate to your question but you need to close the current window before open a new window in MVVM way, see this.

Community
  • 1
  • 1
Red Wei
  • 854
  • 6
  • 22
0

Finally i have fixed this problem using single instance. Instead creating object to MainViewModel, i am getting instance of that ViewModel which already created. Like

var instance=MainWindowViewModel.GetInstance();

GetInstance() will return the instance of the MainWindowViewModel which created already.

BALA G
  • 75
  • 10