Adding Key Bindings to WPF Custom MenuItems

Topics: Developer Forum
Jul 27, 2011 at 2:46 PM

How do we add a Keyboard short cut to the custom menuitem in WPF? The following is my xaml code:

<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="266" Width="Auto" ItemsSource="{Binding}" Name="surveyParametersDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" HorizontalAlignment="Center" VerticalAlignment="Center" GridLinesVisibility="All" HorizontalGridLinesBrush="#FF968F8F" VerticalGridLinesBrush="#FFEBEBEB" Margin="7,-2,-7,36">
                   
<DataGrid.Columns>
                       
<DataGridTextColumn x:Name="iDColumn" Binding="{Binding Path=ID}" Header="ID" Width="Auto" IsReadOnly="True" />
                       
<DataGridTextColumn x:Name="foldColumn" Binding="{Binding Path=Results.Fold}" Header="Fold" Width="Auto" IsReadOnly="True" />
                       
<DataGridTextColumn x:Name="costColumn" Binding="{Binding Path=Results.TimeTaken, Converter={StaticResource TimeTakenConverter}}" Header="Cost" Width="Auto" IsReadOnly="True" />
                       
<DataGridTextColumn x:Name="execTimeColumn" Binding="{Binding Path=Results.ExecTime}" Header="Time of Execution" Width="20*" IsReadOnly="True" />
                   
</DataGrid.Columns>
                   
<DataGrid.ContextMenu>
                       
<ContextMenu>
                           
<MenuItem Command="ApplicationCommands.SelectAll">
                               
<MenuItem.Icon>
                                   
<Image Source="selectAll.ico" />
                               
</MenuItem.Icon>
                           
</MenuItem>
                           
<MenuItem Command="ApplicationCommands.Copy">
                               
<MenuItem.Icon>
                                   
<Image Source="copy.ico" />
                               
</MenuItem.Icon>
                           
</MenuItem>
                           
<MenuItem Header="_GraphIt" Click="menuItem_GraphIt_Click">
                               
<!--<MenuItem.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <AccessText>_G</AccessText>
                                        <TextBlock>
                                            <Span BaselineAlignment="Subscript" FontSize="8">2</Span>GraphIt
                                        </TextBlock>
                                    </StackPanel>
                                </MenuItem.Header>-->

                               
<MenuItem.Icon>
                                   
<Image Source="graph.ico" />
                               
</MenuItem.Icon>
                           
</MenuItem>
                       
</ContextMenu>
                   
</DataGrid.ContextMenu>
               
</DataGrid>

I also tried to do keybindings but am unable to achieve it. The following code is what I tried:

<!--
<Window.CommandBindings>
    <CommandBinding Command="local:CustomCommands.GraphIt" Executed="menuItem_GraphIt_Click" />
</Window.CommandBindings>
<Window.InputBindings>
    <KeyBinding Key="G" Modifiers="Control" Command="local:CustomCommands.GraphIt"/>
</Window.InputBindings>
-->

I am facing problem with this: local:CustomCommands.GraphIt

How do I specify command there?