2016-11-04 5 views
0

WPF MVVM, 으로 작업 중입니다. 기본 윈도우가 있고 버튼 클릭시 usercontrols.based가 거의 없기 때문에 뷰와 뷰 모델을 보여줍니다. 메인 윈도우에서 버튼을 클릭하면 다른보기로 이동할 수 있습니다. 네비게이션 커맨드가 주 윈도우 뷰 모델에만 있기 때문입니다. 그러나 다른 시각에있을 때 (사용자 제어). 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)이있는 홈 스크린입니다 .On 버튼은 해당 뷰 모델 (userListViewModel)이있는 사용자 목록 뷰를로드 중입니다. 이제 사용자보기에서 오전, 사용자보기에서 단추를 클릭하면 다른보기로 리디렉션해야합니다. 리디렉션 코드는 메인 윈도우 모델에 있습니다. 해당 메소드에 액세스하여보기를 변경하는 방법. in.xaml 코드를 작성하여 버튼 클릭을 리디렉션 할 수 있습니다. 하지만 난 viewmodel에서 리디렉션을하고 싶다. –

+2

[Minimal, Complete, Verifiable] (http://stackoverflow.com/help/mcve) 예제를 제공해주십시오. –

답변

0

귀하의 질문이 명확하지 않으므로, 귀하가 필요하다고 생각하는 바를 기반으로합니다.

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

@BALAG 보시다시피 하위 ViewModel에서 MainViewModel에 대한 참조가 부족합니다. 하위 ViewModel은 MainViewModel의 onNav() 메소드를 가리키는'RelayCommand'를 생성해야합니다. – Jai

+0

RelayCommand로 @Jai가 MVVM Light Toolkit의 일부이므로 기본 프레임 워크가 아니라 링크가 포함되어있는 것이 좋습니다. https://mvvmlight.codeplex.com/ – MikeT

+0

@Jai OnNav() 함수를 호출해도 그렇지 않습니다. 일. 실제로 그 기능은 타격을 주지만 내 견해는 변화하지 않습니다. newMainWindowViewModel.OnNav ("매개 변수")와 같이 –