【发布时间】:2015-11-03 22:03:26
【问题描述】:
我想将快捷键绑定到我的用户控件 UserControl.InputBindings。
我没有使用 MVVM 或任何其他模式。我只想使用 XAML 文件的代码隐藏文件绑定此密钥。
【问题讨论】:
标签: c# .net wpf keyboard-shortcuts
我想将快捷键绑定到我的用户控件 UserControl.InputBindings。
我没有使用 MVVM 或任何其他模式。我只想使用 XAML 文件的代码隐藏文件绑定此密钥。
【问题讨论】:
标签: c# .net wpf keyboard-shortcuts
您可以在 XAML 中进行这样的键绑定:
<UserControl.InputBindings>
<KeyBinding Command="{Binding SomeCommand}" Key="F5"/>
</UserControl.InputBindings>
【讨论】:
如果你想分配一个键绑定,并且你想从代码隐藏中做到这一点,这将是最简单的方法:
using System.Windows.Input;
var cmd = new RoutedCommand();
userControl.InputBindings.Add(new KeyBinding(cmd, your_key_gesture_here));
userControl.CommandBindings.Add(new CommandBinding(cmd, your_event_handler_here));
your_key_gesture_here替换为你想要触发的快捷键,your_event_handler_here替换为你想要按键按下时触发的方法。
【讨论】: