Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I am trying to bind an InputGesture to a RoutedCommand bound to a MenuItem inside a ContextMenu.

As shown 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 "Language" button.

snapshot[^]

So far, I have done following:

// my command
C#
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">
	<Button.Style>
		<Style.Triggers>
			<EventTrigger RoutedEvent="Click">
				<EventTrigger.Actions>
					<BeginStoryboard>
						<Storyboard>
							<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
								<DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True" ></DiscreteBooleanKeyFrame>
                                                        </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"></Image>
						</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"></Image>
						</MenuItem.Icon>
					</MenuItem>
				</ContextMenu>
			</Setter.Value>
		</Setter>		
	</Button.Style>
</Button>


I am adding the input gesture to this command as:
C#
DesignerCanvas.SetCultureENCommand.InputGestures.Add(new KeyGesture(Key.F11, ModifierKeys.None));

and finally, I am registering this as:
C#
CommandManager.RegisterClassCommandBinding(typeof(ContextMenu), new CommandBinding(DesignerCanvas.SetCultureENCommand, SetCultureToEN));

I tried to get it done something like this SO 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.
Posted
Comments
Kenneth Haugland 23-Mar-14 16:28pm    
Would be probable that it was designed that way from scratch.
Agent__007 24-Mar-14 2:05am    
@Kenneth Haugland I am not sure what you mean by that, but I am open to redo it from scratch. Any pointers would be really appreciated.
Agent__007 24-Mar-14 4:08am    
@Kenneth Haugland 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?
Kenneth Haugland 24-Mar-14 5:30am    
I think I would remove the one in the context menu, and keep the one in the main window, assuming that would always work?
Agent__007 24-Mar-14 6:00am    
I tried that, but as soon as I remove the binding with the ContextMenu, the MenuItems are being rendered as "disabled".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900