views:

375

answers:

1

I currently have a main View with a Button and a ContentPresenter which is bound to a property of the ViewModel which is another View (and associated ViewModel). Is there way to route a command from the a handler declared in the control loaded in the ContentPresenter? My reason for this is the main View contains the tool bar and the content presenter has the content. I am using the Microsoft MVVM template and the generated DelegateCommand class.

<Window ...>
   <Button x:Name="btnAction" Command="{Binding ActionCommand}" />
   <ContentPresenter Content="{Binding CurrentView}" />
</Window>
+1  A: 

You should create a command object which is a static object on a class that both the window and the control can see.

  public static class MyCommands
  {
     public static RoutedUICommend CoolCommand .....;
  }

Then you can bind the control's Command property to the command object, for example:

<Button Command="cmd:MyCommands.CoolCommand" />

Then you simply need to handle the command binding at the window level using the CommandBinding XAML element.

<CommandBinding Command="cmd:MyCommands.CoolCommand" Executed="My_Handler" />
OJ
Seems like a good idea. Out of curiosity is it a common WPF practice to have a shared command repository for linking stuff together?
jwarzech
It's not that this is a standard practice for linking stuff together. It's more a common practice for when you need to bind commands to command handlers that don't share bindings in another way. This is basically the same mechanism that the built-in WPF comments used (such as ApplicationCommands).
OJ