2015-11-03 94 views
0

我想绑定快捷键到我的用户控件使用 UserControl.InputBindings。 我没有使用MVVM或任何其他模式。我只想使用xaml文件的后面的代码绑定这个键。请任何帮助,一定会感激。非常感谢。Wpf快捷键绑定

回答

0

如果你想要分配键绑定,你想从做后台代码,这将是最简单的方法

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使用您想在按下按键时触发的方法。

+0

感谢您的帮助很大。它使用文件后面的代码解决了问题。 –

0

你可以这样做的关键是这样绑定在XAML

<UserControl.InputBindings> 
     <KeyBinding Command="{Binding SomeCommand}" Key="F5"/> 
    </UserControl.InputBindings> 
+0

这就是我要找的。这将根据DataContext绑定命令并解决我的问题。非常感谢你。 –

+0

如果这有助于标记它的答案 –