7
votes

I have a problem with datagrid control in wpf . when I press enter I want to load selected items in a set of controls, but it seems that datagrid.KeyDown is already handled and it goes to the next item.

I tried using keyup but this event fires when the datagrid keydown had been fired and datagrid goes to the next item.

any idea to handle keydown completely ?

3

3 Answers

9
votes

Use the PreviewKeyDown event instead of KeyDown

0
votes

Maybe KeyDown is handled by a ClassHandler.
Instance listeners come after class listeners.

A good explanation on MSDN.

0
votes

You can add a comma-separated list of handlers to an event...

Private Sub dgMyDataGrid_KeyDown(sender As Object, e As KeyEventArgs) Handles dgMyDataGrid.KeyDown, dgSymbols.PreviewKeyDown
    
    ' Perform event programming here...    
    
End Sub

This triggers handling of the "preview" event at the same time as the keydown event, so you don't have to write more than one module.