DataGrid responds to KeyUp event but IsKeyboardFocused is False

Jul 27, 2009 at 9:13 AM

I'm having a problem with my WPF app when handling the KeyUp event on a DataGrid.  In the KeyUp event handler I am displaying a modal dialog (our own implementation of a message box) asking the user a question with a simple yes/no answer.  However, if I use the enter key on the message box to select the "No" button, the DataGrid still receives the KeyUp event and displays the prompt again.

I thought that if I just checked that the DataGrid had focus in the event handler that this would do the trick, but it doesn't.  Whenever the DataGrid receives the KeyUp event, it seems that the IsKeyboardFocused property on the DataGrid is set to false.  How can the control receive a keyboard event when it doesn't have keyboard focus?

Has anyone else had this problem or have any idea how to fix it?  I'm getting ready to scrap my DataGrid altogether and use a standard ListView / GridView, which seems a shame, but if it's the only way I can get this to work then that's what I'll have to do.

This is my event handler, OpenCustomerForMaintenance() simply displays the message box and fires off a command if the user selects "Yes"

private void CustomerSearchResultsDataGrid_KeyUp(object sender, KeyEventArgs e)
{
    if (CustomerSearchResultsDataGrid.IsKeyboardFocused && (e.Key == Key.Return || e.Key == Key.Enter))
    {
        OpenCustomerForMaintenance();
    }
}

 

 

Thanks

Jul 27, 2009 at 10:02 AM
Edited Jul 27, 2009 at 10:34 AM

I've tried replacing my DataGrid with a standard GridView / ListView combo and still get the same problem.  Also, replacing my dialog with a standard WPF MessageBox doesn't make any difference.

Another problem I'm having with this is that when I hit the enter key with a row selected, the selected row jumps down to the next row, so rather than opening the first customer in the list, it tries to open the second one.  I'm only having these problems when using keyboard navigation, everything works fine with the mouse events.

I tried handling the KeyDown event rather than the KeyUp event, but the KeyDown event isn't raised for the enter or return keys, so is completely useless for what I want (why would it even by implemented that way, surely it's up to the developer to decide which keys they want to respond to???)

 

I'm really starting to hate WPF, it's things like this that make it seem really flaky.  It wasn't my decision to use it, so I'm stuck with it for now  :-(

 

Jul 28, 2009 at 6:19 AM

Try handling PreviewKeyUp instead of KeyUp, and set e.Handled = true inside your if block.

Jul 28, 2009 at 8:37 AM

It turns out that the problem is with the Button on the MessageBox - WPF Buttons respond to the KeyDown event, leaving the following KeyUp event to be handled (or not) elsewhere, which means that it's getting handled by the DataGrid when it really shouldn't be.  Setting e.Handled to true won't fix the problem unfortunately, as I do need the DataGrid to respond to the KeyUp event.

I just can't believe how the Button has been implemented, surely it should be waiting for the KeyUp event to occur rather than just dismissing the MessageBox upon receiving the KeyDown event?