3

I am new to wpf application and I am working on application and i have created a menu Now i want to function menu items event on short key ctrl+o, ctrl+n etc. How can i do it.please give me in details.

Anu
  • 753
  • 2
  • 13
  • 22

2 Answers2

3

You can so it in the following way....

In Xaml file

<Window x:Class="FocusDemo.Window1"  
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    xmlns:local="clr-namespace:FocusDemo"  
Title="Window1" Height="300" Width="300">  

<Window.CommandBindings>  
    <CommandBinding   
        Command=   
        "{x:Static   
        local:Window1.CustomRoutedCommand}"                       
        Executed="ExecutedCustomCommand"   
        CanExecute="CanExecuteCustomCommand" >  
    </CommandBinding>  
</Window.CommandBindings>  
<Window.InputBindings>  
    <KeyBinding   
        Command=  
        "{x:Static   
        local:Window1.CustomRoutedCommand}"   
        Key="S"   
        Modifiers="Control"/>  
</Window.InputBindings>
<Grid>
<!--Your Controls-->
</Grid>
</Window>  

In the Code behind file

    /// <summary>  
/// Interaction logic for Window1.xaml  
/// </summary>  
public partial class Window1 : Window  
{  
    public static RoutedCommand CustomRoutedCommand = new RoutedCommand();    
    public Window1()  
    {             
        InitializeComponent();       
    }  
    #region  
    public void ExecutedCustomCommand(object sender, ExecutedRoutedEventArgs e)  
    {  
        MessageBox.Show("Ctrl+S");  
    }  


    public void CanExecuteCustomCommand(object sender,  
        CanExecuteRoutedEventArgs e)  
    {  
        e.CanExecute = true;  
    }  
    #endregion   
    
}  

Source : Click here

Please dont forget to mark the answer if its correct

Community
  • 1
  • 1
Ankesh
  • 4,847
  • 4
  • 38
  • 76
0

I know it is not exact answer to the question, but probably anybody like me was searching for the way to put ANY keyboard shortcuts to menu items (command buttons) like Alt+O, Alt+N. In this case, you can just put undescore character (_) before the shortcut character in the item name.

LoBo
  • 212
  • 1
  • 2
  • 9