Return to Snippet

Revision: 31027
at August 27, 2010 14:38 by jiewmeng


Initial Code
// create properties to represent program features
public static class Commands
{
    public static readonly RoutedCommand Foo = new RoutedCommand();
}

// 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) { ... }

Initial URL


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

Initial Title
C#/WPF: Routed Commands

Initial Tags


Initial Language
C#