【发布时间】:2011-06-27 22:58:12
【问题描述】:
我在窗口内有 CommandBinding 和 KeyBindings。按下 Alt 修饰符激活命令。是否可以使用此绑定显示下划线符号?
ps 我不想在文本中使用下划线符号,请看这里为什么WPF hot key firing without modifier
【问题讨论】:
标签: wpf
我在窗口内有 CommandBinding 和 KeyBindings。按下 Alt 修饰符激活命令。是否可以使用此绑定显示下划线符号?
ps 我不想在文本中使用下划线符号,请看这里为什么WPF hot key firing without modifier
【问题讨论】:
标签: wpf
您可以简单地将项目的内容以下划线开头;按下 Alt 时,下划线将变为可见。
<Button Height="100" Width="200" Content="_MyButton"/>
如果关注的是在未按下 Alt 键修饰符时触发Command;您可以按照Atanas 的说明修改此行为。
public Window1()
{
InitializeComponent();
EventManager.RegisterClassHandler(typeof(UIElement), AccessKeyManager.AccessKeyPressedEvent, new AccessKeyPressedEventHandler(OnAccessKeyPressed));
}
private static void OnAccessKeyPressed(object sender, AccessKeyPressedEventArgs e)
{
if (!e.Handled && e.Scope == null && (e.Target == null))
{
//if alt key is not in use handle event to prevent behavior without alt key
if ((Keyboard.Modifiers & ModifierKeys.Alt) != ModifierKeys.Alt)
{
e.Target = null;
e.Handled = true;
}
}
}
这将防止Command 在未按下修改键时触发Alt。
【讨论】: