This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Commanding - Keyboard Shortcuts

The one hundred and sixty-sixth part of the Windows Presentation Foundation Fundamentals tutorial takes another look at WPF commanding. This article describes how keyboard shortcuts can be used to execute commands, using key bindings.

Key Bindings

In the previous two articles we have seen how you can link buttons, menu items and similar interactive controls to built-in commands or custom commands that you develop yourself. Commands include the functionality executed when they are invoked and additional code that controls when they can be used and when they are disabled. All of the examples that we have seen involved executing commands in response to the user manipulating a control.

Another common way to launch a control's code is in response to a key, or a combination of keys, being pressed. This allows users who are more comfortable with the keyboard, or those who cannot easily use a mouse or other pointing device, to execute commands quickly. You configure these commands using key bindings.

A key binding is similar to the bindings we have already seen, which launch commands. However, rather than including a binding expression within a control's Command property, key bindings are added as separate, KeyBinding objects. They can be applied to any item that inherits from the UIElement class, allowing you to create bindings against almost any part of a logical tree.

To demonstrate the use of key bindings we need a sample program. Create a new WPF application in Visual Studio, naming the project, "KeyBindingDemo". Once the new solution is prepared, replace the XAML in the main window with the following code:

<Window x:Class="KeyBindingDemo.MainWindow"
        Title="KeyBinding Demo" Height="150" Width="200">
        <Button Margin="45" Command="{Binding MessageCommand}">Click Me</Button>

The window is very simple, containing a single button that is bound to a command. Let's create the class for this command. Add a new class named, "MessageCommand", with the following code:

public class MessageCommand : ICommand
    public void Execute(object parameter)
        string msg;

        if (parameter == null)
            msg = "Button Clicked!";
            msg = parameter.ToString();


    public bool CanExecute(object parameter)
        return true;

    public event EventHandler CanExecuteChanged;

The command allows you to understand how it was called by showing a message box. When called without a parameter, a hard-coded message is shown. If a command parameter is included, it is used in the message. To allow the window access to the command, we need a data context class. Create the following class, which simply surfaces a MessageCommand instance.

public class MyDataContext
    ICommand _messageCommand = new MessageCommand();

    public ICommand MessageCommand
        get { return _messageCommand; }

To complete the basic program, modify the window's constructor so that its data context is set to a new instance of the MyDataContext class.

public MainWindow()
    DataContext = new MyDataContext();

To ensure the program is operating correctly, start it and click the button. You should see the message, "Button Clicked!".

18 June 2015