internetmw
internetmw

Reputation: 699

Override default keybindings in Listbox

I currently have a listbox in wpf c# and added some keybindings to the keydown event handler of my main window. It's event handlers 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?

Upvotes: 2

Views: 1419

Answers (1)

Lukasz Madon
Lukasz Madon

Reputation: 15004

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.

Upvotes: 1

Related Questions