Adding shortcut keys to WPF Application

To add shortcut keys to a WPF application I used the following:

<Window x:Class="ProjectName.MainWindow"
    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:local="clr-namespace:ProjectName"
    mc:Ignorable="d"
    Title="MainWindow" 
    Height="350" Width="525">
 
    <Window.CommandBindings>
        <CommandBinding Command="local:MainWindow.NewItem" Executed="MenuItem_Click" />
    </Window.CommandBindings>
 
    <Window.InputBindings>
        <KeyBinding Command="local:MainWindow.NewItem" Key="N" Modifiers="Ctrl" />
    </Window.InputBindings>
 
    <Grid>
        <Menu HorizontalAlignment="Stretch" VerticalAlignment="Top">
            <MenuItem Header="_File">
                <MenuItem Header="_New Item" Command="local:MainWindow.NewItem" InputGestureText="Ctrl+N" />
            </MenuItem>
        </Menu>
    </Grid>
 
</Window>

The Window.CommandBindings contains the mapping to the commands. It also has the method to execute in Executed.

The Window.InputBindings contains the mappings to the Keys used for shortcuts.

The MenuItem contains the Header as well as the Shortcut display using InputGestureText.

namespace ProjectName
{
    public partial class MainWindow : Window
    {
        public static readonly RoutedUICommand NewItem = new RoutedUICommand("NewItem", "NewItem", typeof(MainWindow));
 
        /// <summary>
        /// Shared method for MenuItems and Shortcut Keys using Commands
        /// </summary>
        /// <param name="menuItem">Name of the selected item</param>
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            var item = string.Empty;
            var menuItem = sender as MenuItem;
            if (menuItem != null)
            {
                item = menuItem.Header.ToString();
            }
            else
            {
                // Needed to be cast to get to the ExecutedRoutedEventArgs Type
                var executedRoutedEventArgs = e as ExecutedRoutedEventArgs;
                if (executedRoutedEventArgs != null)
                {
                    item = (executedRoutedEventArgs.Command as RoutedUICommand).Text;
                }
            }
            if (!string.IsNullOrEmpty(item))
            {
                ProcessMenuItem(item);
            }
        }
 
        private void ProcessMenuItem(string menuItem)
        {
            switch (menuItem)
            {
                case "NewItem":
                    break;
                case "E_xit":
                    Application.Current.Shutdown();
                    break;
            }
        }
    }
}

The RoutedUICommand is the command that is mapped in the XAML.

The Window.CommandBindings, Window.InputBindings and the MenuItem all mapped to an item. This is how the shortcut is wired up.

The MenuItem_Click is the method that is executed when the shortcut is pressed or the menu is clicked. This method handles getting which item was chosen and passes that to the ProcessMenuItem method.

The ProcessMenuItem then processes the item as needed. As many items can be added as needed.

Comments are closed.