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.
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.