0
votes

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>
1
Be more specific, what u want actually ?AnjumSKhan
@AnjumSKhan. Main window is my home screen with corresponding view model(MainWindowViewModel).On button click am loading userlist view with corresponding viewmodel(userListViewModel). now am in user view, when i click button in user view i have to redirect to another view. Redirection code is in mainwindowviewmodel. how to access that method and change the view. i can able to redirect on button click by writing code in.xaml. but i want to do redirection from viewmodel.BALA G
Please provide a Minimal, Complete, and Verifiable example.Nawed Nabi Zada
The answer Jai provided will do the work. Just register MainWindowViewModel in Study- and CameraCaptureViewModel, and send your MainWindowViewModel as parameterNawed Nabi Zada

1 Answers

0
votes

Your question is not clear enough, so this is based on what I think you need.

MainWindow:

<Window x:Class="MyApp.MainWindow" ...>
...
    <ContentControl Content={Binding MyCurrentViewModel}>
        <ContentControl.Resources>
            <DataTemplate DataType={x:Type vm:UserListViewModel}>
                <view:UserListView />
            </DataTemplate>
            <DataTemplate DataType={x:Type vm:AnotherViewModel}>
                <view:AnotherView />
            </DataTemplate>
            ...
        </ContentControl.Resources>
    </ContentControl>
</Window>

MainViewModel:

private ViewModelBase _myCurrentViewModel;
public ViewModelBase MyCurrentViewModel
{
    get { return _myCurrentViewModel; }
    set
    {
        if (value != _myCurrentViewModel)
        {
            _myCurrentViewModel = value;
            RaisePropertyChanged("MyCurrentViewModel");
        }
    }
}

UserListViewModel:

public class UserListViewModel : ViewModelBase
{
    private MainViewModel MainVM;
    public UserListViewModel(MainViewModel mainVM)
    {
        this.MainVM = mainVM;
    }

    private ICommand _myCommand;
    public ICommand MyCommand
    {
        get
        {
            if (_myCommand = null)
                _myCommand = new RelayCommand(MyExecuteDelegate, true);
            return _myCommand;
        }
    }
    private void MyExecuteDelegate(object parameter)
    {
        // My other logic
        if (this.MainVM != null)
            this.MainVM.MyCurrentViewModel = new AnotherViewModel();
    }
}