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?