2016-11-04 107 views
0

我正在使用WPF MVVM, 我有一個主窗口和很少的usercontrols.based按鈕點擊顯示視圖和viewmodels。當我點擊主窗口中的按鈕時,我可以導航到不同的視圖。因爲導航命令僅在主窗口視圖模型中。但是,當我在不同的角度(用戶控制)。我如何從viewmodel導航。從Viewmodel WPF MVVM視圖重定向

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"); 
    } 

當我加入的鼠標事件的命令,它被重定向。但我不無如何從視圖模型

RegisterView重定向.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)。點擊按鈕加載用戶列表視圖與相應的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的引用。你的子ViewModel應該創建一個'RelayCommand',指向MainViewModel的'onNav()'方法。 – Jai

+0

@Jai as RelayCommand是MVVM Light Toolkit的一部分,而不是包含鏈接的主框架將是一個好主意https://mvvmlight.codeplex.com/ – MikeT

+0

@Jai如果我打電話給OnNav()函數也不是加工。實際上這個功能打了,但我的觀點並沒有改變。像newMainWindowViewModel.OnNav(「參數」) –

相關問題