如何在WPF中为按钮分配快捷键?
谷歌给我的答案是在标准Winforms中使用append _而不是'&‘。
因此,在我完成以下操作后:
<Button Name="btnHelp" Content="_Help"></Button>
我没有发现'H‘有下划线。
这是第一个问题。
第二个问题是,在运行时按Alt +H后如何执行。假设仅显示一个消息框就足够了。
我使用的是C#,WPF
谢谢。
发布于 2012-07-12 23:37:00
使用自己的命令进行按键绑定(+按钮绑定)的解决方案:
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>
和.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
}
}
发布于 2010-07-14 21:04:32
示例代码的快捷方式就是h。在……上面
最初显示快捷键的下划线是Windows设置,不受应用程序控制。在Windows XP中,转到显示属性和外观-> Effects,您将看到一个标记为“在按下Alt键之前隐藏键盘导航的带下划线的字母”的复选框。对于Vista/Win7,我认为他们把这个设置移到了别的地方。
发布于 2013-11-05 09:52:38
我找到的最简单的解决方案是在按钮内粘贴一个标签:
<Button Name="btnHelp"><Label>_Help</Label></Button>
https://stackoverflow.com/questions/3246134
复制相似问题