wpf 按钮如何设置快捷键F1-F6

时间:2026-02-12 07:52:26

1、我们新建一个WPF工程,打开MainWindow.xaml,在MainWindow.xml中先设置以RoutedUICommand的对象资源。

 <Window.Resources>

        <RoutedUICommand x:Key="testF1" Text="TestF1">

            

        </RoutedUICommand>

    </Window.Resources>

2、给设置的UiCommand复制给CommandBindig对象,同时为Command添加执行函数,执行函数就是指触发了这个命令,会执行什么操作。

 <Window.CommandBindings>

        <CommandBinding Command="{StaticResource testF1}" Executed="CommandBinding_Executed"></CommandBinding>

    </Window.CommandBindings>

3、然后,我们把这个CommandBing绑定到快捷键F1上,使程序按F1就会触发命令,

 <Window.InputBindings>

        <KeyBinding Gesture="F1" Command="{StaticResource testF1}"></KeyBinding>

    </Window.InputBindings>

4、最终代码

wpf 按钮如何设置快捷键F1-F6

wpf 按钮如何设置快捷键F1-F6

5、这是我们编译生成程序,按F1就会触发相应的命令了。可以看到

wpf 按钮如何设置快捷键F1-F6

6、如果向F1-F6执行相同的命令,那么把F1-F6绑定相同的命令

 <Window.InputBindings>

        <KeyBinding Gesture="F1" Command="{StaticResource testF1}"></KeyBinding>

  <KeyBinding Gesture="F2" Command="{StaticResource testF1}"></KeyBinding>

    </Window.InputBindings>

如果要执行不同的命令,我们就要创建6个命令对象,原理都是一样的

7、如果还有什么不懂的地方,或者需要原始项目文件什么的,可以给我留言

© 2026 途途旅游
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com