2016-11-04 14 views
0

WPF MVVMを使用しています。 メインウィンドウがあり、ボタンのクリックにはほとんどユーザーコントロールがありません。ビューとビューモデルを表示しています。メインウィンドウのボタンをクリックすると、別のビューに移動できます。ナビゲーションコマンドがメインウィンドウのビューモデルにのみ存在するためです。しかし、異なる視点(ユーザコントロール)にあるとき。どのように私はviewmodelからナビゲートすることができます。WPF MVVMビューのリダイレクトを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"); 
    } 

マウスイベントでコマンドを追加していた場合、それは私がのviewmodel

RegisterViewからリダイレクトする方法を何もいけない.Butリダイレクトされていません.xaml

<DataGrid.InputBindings> 
      <MouseBinding Command="{Binding DataContext.NavCommand,RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
     MouseAction="LeftDoubleClick" 
     CommandParameter="historyimage"/> 
     </DataGrid.InputBindings> 
+0

はuが実際に欲しいもの、具体的に? – AnjumSKhan

+0

@AnjumSKhan。メインウィンドウは、対応するビューモデル(MainWindowViewModel)を持つ私のホーム画面です.Onボタンは、対応するviewmodel(userListViewModel)を持つユーザーリストをロードしています。今私はユーザービューでは、私は別のビューにリダイレクトする必要がありますユーザービューでボタンをクリックしています。リダイレクションコードはmainwindowviewmodelにあります。そのメソッドにアクセスしてビューを変更する方法。私はin.xamlというコードを書いてボタンのクリックをリダイレクトすることができます。しかし、私はviewmodelからのリダイレクトをしたい。 –

+2

[最小、完全、および確認可能](http://stackoverflow.com/help/mcve)の例を入力してください。 –

答えて

0

あなたの質問は、私が必要と思うものに基づいています。

メインウィンドウ:

<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(); 
    } 
} 
+1

@BALAG私が見る限り、あなたのサブビューモデルではMainViewModelの参照が不足しています。あなたのサブViewModelsは、MainViewModelの 'onNav()'メソッドを指し示す 'RelayCommand'を作成する必要があります。 – Jai

+0

RelayCommandとしての@Jaiは、MVVM Light Toolkitの一部です。メインフレームワークへのリンクは含まれていません。https://mvvmlight.codeplex.com/ – MikeT

+0

@Jai OnNav()関数を呼び出すと、それもそうではありませんワーキング。実際にその機能は当てはまるが、私の見解は変化していない。 newMainWindowViewModel.OnNav( "parameter")のように –

関連する問題