C#/WPF: Routed Commands


/ Published in: C#
Save to your folder(s)

reference: [John Smith on WPF: Understanding Routed Commands](http://joshsmithonwpf.wordpress.com/2008/03/18/understanding-routed-commands/)


Copy this code and paste it in your HTML
  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

You need to login to post a comment.