将快捷键分配给 WPF 中的按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3246134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assign Short Cut Key to a button in WPF
提问by yurislav
How to assign short-cut key to a button in WPF?
如何为 WPF 中的按钮分配快捷键?
Googling gave me the answer as to append _ instead of '&' in standard Winforms.
谷歌搜索给了我在标准 Winforms 中附加 _ 而不是 '&' 的答案。
So after I have done as below :
所以在我完成如下操作之后:
<Button Name="btnHelp" Content="_Help"></Button>
I did not find 'H' underlined.
我没有发现“H”有下划线。
That is first issue.
那是第一个问题。
Second issue is that, how to execute that after pressing Alt + H at run-time. Say just to display a message box is sufficient for the example sake.
第二个问题是,如何在运行时按 Alt + H 后执行它。例如,仅显示一个消息框就足够了。
I am using C#, WPF
我正在使用 C#、WPF
Thanks.
谢谢。
回答by jhenninger
This is kind of old, but I ran into the same issue today.
I found that the simplest solution is to just use an AccessText element for the button's content.
这有点老了,但我今天遇到了同样的问题。
我发现最简单的解决方案是只对按钮的内容使用 AccessText 元素。
<Button Command="{Binding SomeCommand}">
<AccessText>_Help</AccessText>
</Button>
When you press the Altkey, the 'H' will be underlined on the button.
When you press the key combination Alt+H, the command that is bound to the button will be executed.
当您按下Alt键时,按钮上的“H”将带有下划线。
当您按下组合键 Alt+H 时,将执行绑定到按钮的命令。
回答by yurislav
Solution for key binding (+ button binding) with own commands:
使用自己的命令进行按键绑定(+ 按钮绑定)的解决方案:
The body of XAML file:
XAML 文件的正文:
<Window.Resources>
<RoutedUICommand x:Key="MyCommand1" Text="Text" />
<RoutedUICommand x:Key="MyCommand2" Text="Another Text" />
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="{StaticResource MyCommand1}"
Executed="FirstMethod" />
<CommandBinding Command="{StaticResource MyCommand2}"
Executed="SecondMethod" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Key="Z" Modifiers="Ctrl" Command="{StaticResource MyCommand1}" />
<KeyBinding Key="H" Modifiers="Alt" Command="{StaticResource MyCommand2}" />
</Window.InputBindings>
<Grid>
<Button x:Name="btn1" Command="{StaticResource MyCommand1}" Content="Click me" />
<Button x:Name="btn2" Command="{StaticResource MyCommand2}" Content="Click me" />
</Grid>
and .CS file:
和 .CS 文件:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
public void FirstMethod(Object sender, ExecutedRoutedEventArgs e)
{
// btn1
}
public void SecondMethod(Object sender, ExecutedRoutedEventArgs e)
{
// btn2
}
}
回答by Wallstreet Programmer
The shortcut is just h for your sample code. On
您的示例代码的快捷方式只是 h 。在
To initially show underlines for shortcuts is a Windows setting and not controlled by an app. In Windows XP, go to Display Properties -> Appearance -> Effects and you will see a checkbox labeled "Hide underlined letters for keyboard navigation until I press the Alt key". For Vista/Win7 I think they moved that setting somewhere else.
最初为快捷方式显示下划线是 Windows 设置,不受应用程序控制。在 Windows XP 中,转到显示属性 -> 外观 -> 效果,您将看到一个标记为“隐藏带下划线的字母以进行键盘导航,直到我按下 Alt 键”的复选框。对于 Vista/Win7,我认为他们将该设置移到了其他地方。
回答by sudarsanyes
To bind keyboard gestures,you need to use the KeyGestureswith commands. Check out commandsfor more information on this. Also there are loads of predefined commands which can directly be used in your application (like, Cut, Copy, Paste, Help, Properties, etc.,).
要绑定键盘手势,您需要将KeyGestures与命令一起使用。查看命令以获取更多信息。还有大量预定义命令可以直接在您的应用程序中使用(如剪切、复制、粘贴、帮助、属性等)。
回答by Daniel Crowe
The simplest solution I've found is to stick a label inside the button:
我找到的最简单的解决方案是在按钮内贴一个标签:
<Button Name="btnHelp"><Label>_Help</Label></Button>