locked
WPF Menus - how to assign a custom shortcut key? RRS feed

  • Question

  • Hi,
     I am working in .NET 3.5 with WPF browser application. I have a menu like the below.

    File->New,Open
    Edit->Copy,Paste
    BizLogic->BizAction

    Now I need to assign a shortcut key (Say Ctrl+Shift+B) for the BizAction menu item so that if I am in the application window and I press Ctrl+Shift+B, then the click event handler of this menu item should get called. I have tried with setting the InputGestureText property but it doesn't work. Do I need to do anyother things? Please help me on this regard.

    Thanks,
    Ganesh
    ganesh.p
    Wednesday, June 18, 2008 11:49 AM

Answers

  • Hi,

    You need to bind a Command to your MenuItem and set the input gesture on the command.

    You should check this article :)
    http://www.microsoft.com/belux/msdn/nl/community/columns/jdruyts/wpf_commandpattern.mspx

    Application Developper
    • Proposed as answer by Yann ALET Wednesday, June 18, 2008 4:27 PM
    • Marked as answer by Marco Zhou Tuesday, June 24, 2008 11:18 AM
    Wednesday, June 18, 2008 4:27 PM
  • Hi,

    Many thanks. Have referred some other articles also and tried the below one. It works fine. If possible, please go through it and let me know that everything is OK.


    1. Have declared the following under Window class

       public partial class Window2 : Window  
        {  
            public static RoutedCommand CustomRoutedCommand = new RoutedCommand();  
    .  
    .  

    2. Implemented the ExecuteCustomCommand and CanExecuteCustomCommand inside Window2 class as below
            public void ExecutedCustomCommand(object sender,    ExecutedRoutedEventArgs e)  
            {  
                MessageBox.Show("Custom Command Executed");  
            }  
     
            // CanExecuteRoutedEventHandler that only returns true if  
            // the source is a control.  
            public void CanExecuteCustomCommand(object sender,  
                CanExecuteRoutedEventArgs e)  
            {  
                Control target = e.Source as Control;  
     
                if (target != null)  
                {  
                    e.CanExecute = true;  
                }  
                else 
                {  
                    e.CanExecute = false;  
                }  
            }  
     
    3. Declared the custom namespace in Window tag in XAML page.

    <Window x:Class="WpfApplication2.Window2" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:custom="clr-namespace:WpfApplication2" 
     
        Title="Window2" Height="300" Width="300"
    4. Declared the CommandBinding under Window tag in XAML page.

        <Window.CommandBindings> 
            <CommandBinding Command="{x:Static custom:Window2.CustomRoutedCommand}" 
                        Executed="ExecutedCustomCommand" 
                        CanExecute="CanExecuteCustomCommand" /> 
        </Window.CommandBindings> 

    5. Have associated the command with a Button and a Menu item in XAML page

     <Button Command="{x:Static custom:Window2.CustomRoutedCommand}" 
              Content="Click here"/>  
    .  
    .  
    .  
    <MenuItem Name="mnuNew" Header ="New..." Command="{x:Static custom:Window2.CustomRoutedCommand}" ></MenuItem
    6. Under constructor of Window object, associate the functions and key combinations to the Command object (have tried to associate Ctrl+N key) as below.

           public Window2()  
            {  
     
                InitializeComponent();  
                CommandBinding customCommandBinding = new CommandBinding(  
    CustomRoutedCommand, ExecutedCustomCommand, CanExecuteCustomCommand);  
     
     
                KeyGesture CloseCmdKeyGesture = new KeyGesture(  
        Key.N, ModifierKeys.Control);  
     
                CustomRoutedCommand.InputGestures.Add(CloseCmdKeyGesture);  
    .  
    .  


    Thanks,
    Ganesh.
    ganesh.p
    • Marked as answer by Marco Zhou Tuesday, June 24, 2008 11:18 AM
    Thursday, June 19, 2008 2:52 AM

All replies

  • Hi,

    You need to bind a Command to your MenuItem and set the input gesture on the command.

    You should check this article :)
    http://www.microsoft.com/belux/msdn/nl/community/columns/jdruyts/wpf_commandpattern.mspx

    Application Developper
    • Proposed as answer by Yann ALET Wednesday, June 18, 2008 4:27 PM
    • Marked as answer by Marco Zhou Tuesday, June 24, 2008 11:18 AM
    Wednesday, June 18, 2008 4:27 PM
  • Hi,

    Many thanks. Have referred some other articles also and tried the below one. It works fine. If possible, please go through it and let me know that everything is OK.


    1. Have declared the following under Window class

       public partial class Window2 : Window  
        {  
            public static RoutedCommand CustomRoutedCommand = new RoutedCommand();  
    .  
    .  

    2. Implemented the ExecuteCustomCommand and CanExecuteCustomCommand inside Window2 class as below
            public void ExecutedCustomCommand(object sender,    ExecutedRoutedEventArgs e)  
            {  
                MessageBox.Show("Custom Command Executed");  
            }  
     
            // CanExecuteRoutedEventHandler that only returns true if  
            // the source is a control.  
            public void CanExecuteCustomCommand(object sender,  
                CanExecuteRoutedEventArgs e)  
            {  
                Control target = e.Source as Control;  
     
                if (target != null)  
                {  
                    e.CanExecute = true;  
                }  
                else 
                {  
                    e.CanExecute = false;  
                }  
            }  
     
    3. Declared the custom namespace in Window tag in XAML page.

    <Window x:Class="WpfApplication2.Window2" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:custom="clr-namespace:WpfApplication2" 
     
        Title="Window2" Height="300" Width="300"
    4. Declared the CommandBinding under Window tag in XAML page.

        <Window.CommandBindings> 
            <CommandBinding Command="{x:Static custom:Window2.CustomRoutedCommand}" 
                        Executed="ExecutedCustomCommand" 
                        CanExecute="CanExecuteCustomCommand" /> 
        </Window.CommandBindings> 

    5. Have associated the command with a Button and a Menu item in XAML page

     <Button Command="{x:Static custom:Window2.CustomRoutedCommand}" 
              Content="Click here"/>  
    .  
    .  
    .  
    <MenuItem Name="mnuNew" Header ="New..." Command="{x:Static custom:Window2.CustomRoutedCommand}" ></MenuItem
    6. Under constructor of Window object, associate the functions and key combinations to the Command object (have tried to associate Ctrl+N key) as below.

           public Window2()  
            {  
     
                InitializeComponent();  
                CommandBinding customCommandBinding = new CommandBinding(  
    CustomRoutedCommand, ExecutedCustomCommand, CanExecuteCustomCommand);  
     
     
                KeyGesture CloseCmdKeyGesture = new KeyGesture(  
        Key.N, ModifierKeys.Control);  
     
                CustomRoutedCommand.InputGestures.Add(CloseCmdKeyGesture);  
    .  
    .  


    Thanks,
    Ganesh.
    ganesh.p
    • Marked as answer by Marco Zhou Tuesday, June 24, 2008 11:18 AM
    Thursday, June 19, 2008 2:52 AM