开发者

MVVM Command Routing Between Controls

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>


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" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜