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
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))