将KeyGesture绑定到ContextMenu中的MenuItem [英] Bind a KeyGesture to a MenuItem inside a ContextMenu

查看:97
本文介绍了将KeyGesture绑定到ContextMenu中的MenuItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 InputGesture 绑定到绑定到 MenuItem <的 RoutedCommand / code>在 ContextMenu 中。



如下图所示,我试图将 KeyGesture 绑定到Language-> English / England MenuItem 其中Language是 Button ,English / England是 MenuItem 在语言按钮的 ContextMenu 内。



快照 [ ^ ]



到目前为止,我已完成以下工作:



//我的命令

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

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



在XAML模板中,我有以下内容,


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:

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));



我试过得到它像这个问题 [ ^ ],但似乎 KeyGesture / b> 当我明确点击语言然后按下手势(即明确打开<后) code>文本菜单
)。当我通过 MainWindow 本身按下手势时(即没有明确地打开 ContextMenu ),它无法正常工作。 />


任何帮助都会非常感激。在此先感谢。


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.

推荐答案

这篇关于将KeyGesture绑定到ContextMenu中的MenuItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆