我正在使用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>
更具體,U真正想要的? – AnjumSKhan
@AnjumSKhan。主窗口是我的主屏幕與相應的視圖模型(MainWindowViewModel)。點擊按鈕加載用戶列表視圖與相應的viewmodel(userListViewModel)。現在我在用戶視圖中,當我點擊用戶視圖中的按鈕時,我必須重定向到另一個視圖。重定向代碼位於mainwindowviewmodel中。如何訪問該方法並更改視圖。我可以通過編寫in.xaml代碼重定向按鈕。但我想從viewmodel做重定向。 –
請提供[最小,完整和可驗證](http://stackoverflow.com/help/mcve)示例。 –