/ Published in: C#
reference: [John Smith on WPF: Understanding Routed Commands](http://joshsmithonwpf.wordpress.com/2008/03/18/understanding-routed-commands/)
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// create properties to represent program features public static class Commands { } // in WPF/XAML // reference your project namespace or the namespace containing the Commands class <Window ...xmlns:local="clr-namespace:RoutedCommandDemo"> // setup command bindings <Window.CommandBindings> <CommandBinding Command="{x:Static local:Commands.Foo}" CanExecute="Foo_CanExecute" Executed="Foo_Executed" /> </Window.CommandBindings> // setup keyboard short cuts <Window.InputBindings> <KeyBinding Command="{x:Static local:Commands.Foo}" Key="F" Modifiers="Ctrl" /> </Window.InputBindings> // have your button reference those commands as defined in command bindings <Button Command="{x:Static local:Commands.Foo}" ... /> // in code behind ... have events to handle commands/routed commands void Foo_CanExecute(object sender, CanExecuteRoutedEventArgs e) { ... } void Foo_Executed(object sender, ExecutedRoutedEventArgs e) { ... }