1

I have a WPF application which I am trying to implement in a MVVM pattern (as much as possible). One of my view models opens a child window with code like this:

SomeChildWindow childWin = new SomeChildWindow();
childWin.DataContext = someChildViewModel;
childWin.ShowDialog();

The problem I am having is that a RoutedCommand I am using in SomeChildWindow is not getting received:

<Button Content="Do Work" Command="root:GlobalCommand.DoWork"/>

GlobalCommand is a static class and DoWork is a RoutedUICommand. I have DoWork bound to the my main view model with code like this:

//Method inside the main view model
public void BindGlobalCommands(Window win)
{
    win.CommandBindings.Add( new CommandBinding
        ( GlobalCommand.DoWork, (s, e) => { this.DoSomeWork(); } ) );
}

I did some research and I am quite sure that I am having the same problem as the person who asked this question: WPF Routed Commands and ShowDialog Windows. However, the answer provided to that question requires that the parent view model have direct knowledge of its own view, which mine doesn't

Am I spawning windows in a correct MVVM way?

How can I use RoutedCommand in my child Window?

Community
  • 1
  • 1
DaveS
  • 895
  • 1
  • 8
  • 20
  • Is the command part of the view model? If so, shouldn't the Command use `{Binding ...}' instead of referencing a global command? Please add more details. – Emond Sep 07 '15 at 05:52
  • @ErnodeWeerd, I added some more detail. My intention is to store logic for some global commands in the main view model. That way any child controls can use RoutedUICommands to invoke global commands. Also, I want to avoid duplicate code in case the same command is called from different locations. – DaveS Sep 07 '15 at 06:03

1 Answers1

0

For dialogs I create separate ViewModels that can be part of the main ViewModel.

The main ViewModel instantiates the dialog's VM, creates the Dialog, sets the DataContext and waits for the dialog to close (if it is Modal)

The Dialog uses the commands on the dialog's ViewModel.

Once the Dialog closes, the main ViewModel looks at the result of the Dialog (Yes, No, Cancel, Ok, ...) which can be a property of the dialog's VM. Based on this result the main VM then uses the content of the dialog to execute some command(s).

This way the dialog is reusable and the main VM stays in control.

Emond
  • 50,210
  • 11
  • 84
  • 115
  • I am doing pretty much what you describe here. Except the dialog I am using is more complicated than a simple `MessageBox` and has its own `ListView` with a bunch of buttons. Also the command I am trying to bind to is a `RoutedUICommand`. Like I mentioned in my other comment, I am not using the basic `ICommand` here because the functionality is global to the entire application and might be called from other UserControls/Windows. – DaveS Sep 07 '15 at 16:48
  • @DaveS - And that is my suggestion: put all the complicated stuff in the dialog's VM and use the main VM after the dialog has been closed. If there is overlapping functionality perhaps that should be factored out of these two view models and be referenced (e.g. in a repository) – Emond Sep 07 '15 at 17:25