2015-11-03 97 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

如果這有助於標記它的答案 –