MVVM Show View Pattern 2 Command Parameter

1 minute read

Reference code

Display using a command that implements the ICommand interface.

Let’s take a look at the code.

You specify a command and specify the window name in its parameter.

  <Button Content="Open Window"
                    Command="{Binding OpenWindowCommand}"
                    CommandParameter="{x:Type local:TestWindow}"/>


   public class MainWindowViewModel
        public OpenWindowCommand OpenWindowCommand { get; private set; }
        public ShowDialogCommand ShowDialogCommand { get; private set; }

        public MainWindowViewModel()
            OpenWindowCommand = new OpenWindowCommand();
            ShowDialogCommand = new ShowDialogCommand(PostOpenDialog, PreOpenDialog);

Command class (the part corresponding to Model?)

I’m showing here.
Except for View and ViewModel, everything is considered as Model, so I think it’s okay to recognize this part as Model, but what about it? (In the VM code, it is not equivalent to model, it is not just View / ViewModel)

   public class OpenWindowCommand : ICommand

        protected virtual void OpenWindow(Window wnd)

What I don’t understand

In this process flow, where is TestWindow confirmed and displayed?
Is it a View, a ViewModel, or an OpenWindowCommand?
I think this is a View that specifies the names ** x: Type local: TestWindow and TestWindow **, but is it different?
Both ViewModel and OpenWindowCommand are the parts that are given arguments and perform processing, so they are not fixed as TestWindow.
Therefore, it seems to me that it is the View’s MainWindow that knows the View called TestWindow. Then, it is the same as basic pattern 1. Basic pattern 1 is code-behind and displays the View specified from View, but this pattern uses a command in XAML to display the View specified from View.

When changing where to specify TestWindow

But in XAML

CommandParameter="{x:Type local:TestWindow}"

Without specifying

  • Specify TestWindow in MainWindowViewModel → VM knows another View
  • Specify TestWindow with OpenWindowCommand → M knows another View

I think it will be different, but is this way of thinking correct?

Article schedule

Tags: ,