Intersoft ClientUI Documentation
How-to: Add Keyboard Access to a Command using KeyBinding

The following examples show how to add keyboard access to a routed command by adding a KeyBinding to the InputBindings collection.

Example

Description

Using commanding pattern in your application helps you design user interface that works consistently and reliably. One of the benefits of using commanding is that you can add an input gesture to a command, such as a keyboard key access, a mouse event or other input devices manipulation.

The input binding takes advantage of the command's CanExecute mechanism which is the essence of the commanding pattern itself. When an associated input matches a command in the binding, the command will be queried whether it can be executed, which is represented by its CanExecute value in the event data. If the CanExecute value is false, the input binding will not execute the matched command. This is true for other input gestures as well, which makes commanding an idea solution to create a rich user interface that works consistently and reliably.

This example shows how to assign a keyboard access key (shortcut) to a routed command by defining the KeyBinding and the InputBindings collection in XAML.

Code

XAML
Copy Code
<Intersoft:UXPage 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:Intersoft="http://intersoft.clientui.com/schemas"
        xmlns:Commands="clr-namespace:ClientUIApplication_Docs.Commands"
        mc:Ignorable="d"
        x:Class="ClientUIApplication_Docs.RoutedCommands.KeyBinding" 
        Title="CommandOverview Page"
        d:DesignWidth="640" d:DesignHeight="480">

        <Grid x:Name="LayoutRoot">

            <Intersoft:CommandManager.CommandBindings>
                <Intersoft:CommandBindingCollection>
                    <Intersoft:CommandBinding Command="Commands:EditingCommands.Cut"
                                              CanExecute="CutCmdCanExecute"
                                              Executed="CutExecuted"/>
                </Intersoft:CommandBindingCollection>
            </Intersoft:CommandManager.CommandBindings>
                        
            <Intersoft:CommandManager.InputBindings>
                <Intersoft:InputBindingCollection>
                    <Intersoft:KeyBinding Command="Commands:EditingCommands.Cut" Gesture="Ctrl+Shift+X" />
                </Intersoft:InputBindingCollection>
            </Intersoft:CommandManager.InputBindings>

            <Intersoft:DockPanel Name="dockPanel1" FillChildMode="Custom">
                <Intersoft:UXToolBar Name="toolBar1" Intersoft:DockPanel.Dock="Top">
                    <Intersoft:UXToolBarButton Name="btn_Cut" Command="Commands:EditingCommands.Cut" DisplayMode="Image" Icon="../Images/CutHS.png" ToolTipService.ToolTip="Cut"/>
                </Intersoft:UXToolBar>
                <Intersoft:UXTextBox Name="textBox1" Text="Type your text here..." Intersoft:DockPanel.IsFillElement="True" />
            </Intersoft:DockPanel>

        </Grid>
<Intersoft:UXPage>
C#
Copy Code
using System.Windows;
using ClientUIApplication_Docs.Commands;
using Intersoft.Client.Framework.Input;
using Intersoft.Client.UI.Navigation;

namespace ClientUIApplication_Docs.RoutedCommands
{
    public partial class KeyBinding : UXPage
    {
        public KeyBinding()
        {
            // It is important to initialize the static EditingCommands class
            // before InitializeComponent is called
            EditingCommands.Initialize();
            InitializeComponent();
        }

        private void CutCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void CutExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            string command = ((RoutedCommand)e.Command).Name;
            string target = ((FrameworkElement)e.OriginalSource).Name;

            MessageBox.Show("The " + command + " command has been invoked on target object " + target);
        }
    }
}

To learn how to create the routed commands for EditingCommands used in the above example, see How-to: Create Routed Commands In a Static Class.

Example

Description

If a command uses a certain identical key throughout the entire application, you can define the KeyGesture in the command definition directly. This eliminates the need to define the same KeyGesture in multiple places of your views. For example, the Ctrl+C is used consistently in many applications to represent a Copy command.

The following code example shows how to add the KeyGesture to the command definition in the static class constructor.

Code

C#
Copy Code
        static EditingCommands()
        {
            Cut.InputGestures.Add(new KeyGesture(Key.X, ModifierKeys.Control | ModifierKeys.Shift));
            Copy.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Control | ModifierKeys.Shift));
            Paste.InputGestures.Add(new KeyGesture(Key.V, ModifierKeys.Control | ModifierKeys.Shift));
        }

To learn how to create the routed commands for EditingCommands used in the above example, see How-to: Create Routed Commands In a Static Class.

See Also

Concepts

Other Resources