I currently have a listbox in wpf c# and added some keybindings to the keydown event handler of my main window. It's event handle开发者_StackOverflow中文版rs for the spacebar and the up/down keys. However, when my listbox has focus, these bindings are not initiated. How can I disable these default keybindings (without entering a e.handle = true so the entire keystroke is disabled) for this control and run my own code?
The default keybindings do not use events directly but commands. What you can do is to define commands and attach events to them.
<Window.Resources>
<CommandBinding x:Key="NewBinding" Command="ApplicationCommands.New"
Executed="NewCommand" CanExecute="CanExecuteNew">
</CommandBinding>
</Window.Resources>
<ListBox.CommandBindings>
<StaticResource ResourceKey="NewBinding"></StaticResource>
</ListBox.CommandBindings>
To override default keybindings the syntax is :
<Window.InputBindings>
<KeyBinding Key="" Modifiers="" Command="" />
</Window.InputBindings>
If you are new to this try to get some overview WPF Commanding Overview and Advanced WPF you are going to have hard time to do so with events.
精彩评论