Posted By

jiewmeng on 08/27/10


Tagged

routed-commands


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Tyster


C#/WPF: Routed Commands


 / Published in: C#
 

  1. // create properties to represent program features
  2. public static class Commands
  3. {
  4. public static readonly RoutedCommand Foo = new RoutedCommand();
  5. }
  6.  
  7. // in WPF/XAML
  8. // reference your project namespace or the namespace containing the Commands class
  9. <Window ...xmlns:local="clr-namespace:RoutedCommandDemo">
  10.  
  11. // setup command bindings
  12. <Window.CommandBindings>
  13. <CommandBinding Command="{x:Static local:Commands.Foo}"
  14. CanExecute="Foo_CanExecute"
  15. Executed="Foo_Executed" />
  16. </Window.CommandBindings>
  17.  
  18. // setup keyboard short cuts
  19. <Window.InputBindings>
  20. <KeyBinding Command="{x:Static local:Commands.Foo}"
  21. Key="F"
  22. Modifiers="Ctrl" />
  23. </Window.InputBindings>
  24.  
  25. // have your button reference those commands as defined in command bindings
  26. <Button Command="{x:Static local:Commands.Foo}" ... />
  27.  
  28. // in code behind ... have events to handle commands/routed commands
  29. void Foo_CanExecute(object sender, CanExecuteRoutedEventArgs e) { ... }
  30. void Foo_Executed(object sender, ExecutedRoutedEventArgs e) { ... }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: jiewmeng on August 27, 2010

also referenced: StackOverflow: C#/WPF Implement Keyboard Shortcuts

You need to login to post a comment.