1

I am trying to bind an InputGesture to a RoutedCommand bound to a MenuItem inside a ContextMenu.

As shown in image below, I am trying to bind a KeyGesture to Language->English/England MenuItem where Language is a Button and English/England is a MenuItem inside the ContextMenu of the Language button.

temp

So far, I have done following:

-> my command

public static RoutedCommand SetCultureENCommand = new RoutedCommand("SetCultureENCommand", typeof(ContextMenu));

in XAML template, I have following

<Button x:Name="btnCulture" Grid.Column="2" Grid.Row="0">
<!-- other stuff here -->
<Button.Style>
    <Style TargetType="{x:Type Button}">
        <Style.Triggers>
            <EventTrigger RoutedEvent="Click">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
                                <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True" />
                            </BooleanAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Style.Triggers>
        <Setter Property="ContextMenu">
            <Setter.Value>
                <ContextMenu>
                    <MenuItem Header="German/Germany" Command="{x:Static DesignerItems:DesignerCanvas.SetCultureDECommand}" Style="{StaticResource MenuItemStyle}">
                        <MenuItem.Icon>
                            <Image Source="/myImageDir/de.png" Width="20"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="English/England" Command="{x:Static DesignerItems:DesignerCanvas.SetCultureENCommand}" Style="{StaticResource MenuItemStyle}">
                        <MenuItem.Icon>
                            <Image Source="/myImageDir/gb.png" Width="20"/>
                        </MenuItem.Icon>
                    </MenuItem>
                </ContextMenu>
            </Setter.Value>
        </Setter>
    </Style>
</Button.Style>

I am adding the input gesture to this command as:

DesignerCanvas.SetCultureENCommand.InputGestures.Add(new KeyGesture(Key.F11, ModifierKeys.None));

and finally, I am registering this as:

CommandManager.RegisterClassCommandBinding(typeof(ContextMenu), new CommandBinding(DesignerCanvas.SetCultureENCommand, SetCultureToEN));

I tried to get it done something like this question, but it seems the KeyGesture is only working when I explicitely click the Language first and then press the gesture (i.e. after explicitely opening the ContextMenu). It is not working when I press the gesture through the MainWindow itself (i.e. without explicitely opening the ContextMenu).

Any help would be really appreciated. Thanks in advance.

Community
  • 1
  • 1
Agent007
  • 2,720
  • 3
  • 20
  • 24
  • Have you register `KeyBinding` with `InputBindings` of your window? – Rohit Vats Mar 22 '14 at 07:47
  • @RohitVats No, since it's for a `MenuItem` inside a `ContextMenu`, I am not adding any `Window.CommandBindings` and hence not `Window.InputBindings`. Am I missing something? – Agent007 Mar 22 '14 at 07:59
  • `It is not working when I press the gesture through the MainWindow itself (i.e. without explicitely opening the ContextMenu).` - In case you want it to run directly from MainWindow, you have to register it with your window. – Rohit Vats Mar 22 '14 at 08:05
  • @RohitVats Ok, I have got it working by adding a `CommandBinding` in `MainWindow` as well (along with the `ContextMenu`) using `this.CommandBindings.Add(new CommandBinding(DesignerCanvas.SetCultureENCommand, SetCultureEN));` and then `this.InputBindings.Add(new InputBinding(DesignerCanvas.SetCultureENCommand, new KeyGesture(Key.G, ModifierKeys.Control)));`. Although it seems to be working, is there any downside of this i.e. registering a `CommandBinding` with `MainWindow` along with `ContextMenu` that I am missing? – Agent007 Mar 24 '14 at 08:05

0 Answers0