New to Telerik UI for WPF? Download free 30-day trial

Enabling Keyboard Shortcuts for RadMenu

Environment

Product Version
RadMenu for WPF Current

Description

How to enable keyboard shortcuts for RadMenu items using the WPF KeyBinding objects.

Solution

In WPF, in order to recieve a KeyDown event, the corresponding element needs to be focused. Since RadMenu is usually not constantly focused, you cannot rely on its KeyDown event. Instead, you can use the KeyDown of the host Window.

To add shortcuts for the menu, you can add KeyBinding objects in the host Window's InputBindings collection.

<Window x:Class="MyWpfApplication.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:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
        mc:Ignorable="d" 
        Title="MainWindow" Height="450" Width="800"> 
    <Window.InputBindings> 
        <KeyBinding Gesture="Ctrl+N" Command="{Binding MyAddNewCommand}"/> 
        <KeyBinding Gesture="Ctrl+E" Command="{Binding MyEditCommand}"/> 
        <KeyBinding Gesture="Ctrl+S" Command="{Binding MySaveCommand}"/> 
    </Window.InputBindings> 
    <Grid> 
        <telerik:RadMenu VerticalAlignment="Top"> 
            <telerik:RadMenuItem Header="New" InputGestureText="Ctrl+N" /> 
            <telerik:RadMenuItem Header="Edit" InputGestureText="Ctrl+E"/> 
            <telerik:RadMenuItem Header="Save" InputGestureText="Ctrl+S"/> 
        </telerik:RadMenu> 
    </Grid> 
</Window> 
In this article