#1,086 – Defining a Key Binding in XAML

You define a KeyBinding object to bind a key gesture (i.e. keypress) to a particular command.  You can do this in code by creating a KeyBinding instance and adding it to an UI elements InputBindings collection.

You can also define a key binding in XAML, as shown below.  You use a <KeyBinding> element for each key binding.

<Window x:Class="WpfApplication1.MainWindow"
        Title="Commands" Width="320" Height="220">

        <CommandBinding Command="ApplicationCommands.Open"
        <KeyBinding Command="ApplicationCommands.Open"


Here is the code-behind, containing methods for Executed and CanExecute.

        public void Executed_Open(object sender, ExecutedRoutedEventArgs e)
            MessageBox.Show("Executing the Open command");

        public void CanExecute_Open(object sender, CanExecuteRoutedEventArgs e)
            e.CanExecute = true;

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #1,086 – Defining a Key Binding in XAML

  1. Pingback: Dew Drop – June 4, 2014 (#1790) | Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: