WPF中为button添加快捷键(ShortCut)的方法
引言
如果你想为WPF中的button添加某些快捷键,就像CTRL + F
,那么你直接在Google中搜索Assign shortcut on button with wpf类似的信息的话,那么搜索出来的一般解决方法就如下所示-->
<Button Name="btnHelp" Content="_Help"></Button>
这样的效果就是按ALT + H
的时候会类似于点击这个button,但是这样实现会有局限性。
为此自己参考了许多资料后,给出了使用InputBindings和KeyBinding实现button的快捷键设置的方案。
在示例程序中,定义了两个button,分别是Add和Sub,两个button的快捷键分别是CTRL + A
和 CTRL + S
,作用分别将右边的数字加1或者减1。
实现
xaml 实现
<Window x:Class="ShortCutButton.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ShortCutButton"
mc:Ignorable="d"
Title="ShortCutDemo" Height="100" Width="207" SizeToContent="WidthAndHeight" ResizeMode="NoResize">
<!-- define command -->
<Window.Resources>
<ResourceDictionary x:Uid="CommandDict">
<RoutedCommand x:Uid="AddCommand" x:Key="AddCommand" />
<RoutedCommand x:Uid="SubCommand" x:Key="SubCommand" />
</ResourceDictionary>
</Window.Resources>
<Window.CommandBindings>
<CommandBinding x:Uid="AddCommandParameter" Command="{StaticResource AddCommand}" CanExecute="Add_CanExecute" Executed="Add_Executed" />
<CommandBinding x:Uid="SubCommandParameter" Command="{StaticResource SubCommand}" CanExecute="Sub_CanExecute" Executed="Sub_Executed" />
</Window.CommandBindings>
<!-- assign shortcut with command -->
<Window.InputBindings>
<KeyBinding x:Uid="AddKeyBinding" Key="A" Modifiers="Ctrl" Command="{StaticResource AddCommand}" />
<KeyBinding x:Uid="SubkeyBinding" Key="S" Modifiers="Ctrl" Command="{StaticResource SubCommand}"/>
</Window.InputBindings>
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Top">
<Grid ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<WrapPanel Grid.Column="0" Orientation="Vertical">
<!-- margin left top right bottom -->
<Button Width="60" Content="Add" FontSize="12" FontStyle="Normal" Margin="5, 5" Background="Azure" Command="{StaticResource AddCommand}">
</Button>
<Button Width="60" Content="Sub" FontSize="12" FontStyle="Normal" Margin="0,0,0,5" Background="Azure" Command="{StaticResource SubCommand}">
</Button>
</WrapPanel>
<StackPanel Grid.Column="1">
<Label Grid.Column="1" Background="Bisque" FontWeight="Bold" Margin="5,5,5,0">Show Result</Label>
<TextBlock Grid.Column="1" Name="showRes" FontSize="12" Text="0" Margin="5, 5" />
</StackPanel>
</Grid>
</StackPanel>
</Grid>
</Window>
代码逻辑实现
private void Add_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void Add_Executed(object sender, ExecutedRoutedEventArgs e)
{
var res = Convert.ToInt32(showRes.Text);
res++;
showRes.Text = Convert.ToString(res);
}
private void Sub_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (showRes != null && !string.IsNullOrEmpty(showRes.Text))
{
e.CanExecute = true;
}
}
private void Sub_Executed(object sender, ExecutedRoutedEventArgs e)
{
var res = Convert.ToInt32(showRes.Text);
res--;
showRes.Text = Convert.ToString(res);
}
最终效果
当你按下CTRL+A
的时候,右边数字会自动加1;
当你按下CTRL+S
的时候,右边数字会自动减1;
参考资料
作者:醉曦
欢迎任何形式的转载,但请务必注明出处。
限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】