Am working with WPF MVVM, I have a main window and few usercontrols.based on the button click am showing the view and viewmodels. When i click button in main window i can navigate to different view. because navigation command is in mainwindow viewmodel only. But when am in different view(user control) . How can i navigate from viewmodel.
MainwindowViewmodel
public class MainWindowViewModel : BindableBase, INotifyPropertyChanged
{
private StudyViewModel _studyViewModel = new StudyViewModel();
private ImageCaptureViewModel _imageCaptureViewModel = new ImageCaptureViewModel();
private RegisterViewModel _registerViewModel = new RegisterViewModel();
private BindableBase _CurrentViewModel;
public BindableBase CurrentViewModel
{
get { return _CurrentViewModel; }
set { SetProperty(ref _CurrentViewModel, value); OnPropertyChanged("_CurrentViewModel"); }
}
public MainWindowViewModel()
{
NavCommand = new RelayCommand<string>(onNav);
onNav("study");
}
//This is the command am using in xaml to redirect view.
public RelayCommand<string> NavCommand { get; private set; }
private void onNav(string destination)
{
switch (destination)
{
case "study":
CurrentViewModel = _studyViewModel;
break;
case "capture":
CurrentViewModel = _imageCaptureViewModel;
break;
case "register":
CurrentViewModel = _registerViewModel;
break;
default:
CurrentViewModel = _studyViewModel;
break;
}
}
}
RegisterViewModel
public void RegisterPatient(string action)
{
if (action == "addnew")
{
}
else
{
if (StartImaging)
{
//Have to redirect to other screen.
}
}
var a = PatientToAdd;
MessageBox.Show("triggered");
}
When am adding command in mouse event it is redirecting .But i dont no how to redirect from viewmodel
RegisterView.xaml
<DataGrid.InputBindings>
<MouseBinding Command="{Binding DataContext.NavCommand,RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
MouseAction="LeftDoubleClick"
CommandParameter="historyimage"/>
</DataGrid.InputBindings>