Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1150
Modify Enter behavior to commit current cell only and tab to next cell.
posted

Hello,

I modified the behavior of the XamDataGrid on Enter key press.
Instead of committing the record, I want to commit the current cell and tab to the next cell.
The attached sample demonstrates this. 

To get this behavior, I intercept the PreviewKeyDown event,
and when it is the return key, the CellNextByTab command is executed instead.

This works great on simple data cells.
But when in a DopDown cell, the selected value is not committed to the cell any more.
I know why, because the DropDown never gets the Enter key. But I do not know how to fit both needs together.

Any idea?
Thank you! 

XamDataGridEnterBehavior.zip
Parents
  • 34510
    Verified Answer
    Offline posted

    Hello J_Feuerstein,

    Place the ExecuteCommand code inside a Dispatcher. 

    // Modify Return behavior
    if (e.Key == Key.Return && Keyboard.Modifiers == ModifierKeys.None)
    {
        Dispatcher.BeginInvoke(new Action(() =>
        {
            DataGrid.ExecuteCommand(DataPresenterCommands.CellNextByTab);
        }), null);
    }

     

    In order for the combobox to make a selection, it needs to see the Enter key pressed.  There's no real way to force the selection otherwise without having to dig around inside the dropdown items to see which one is highlighted and then set that as the selected item.  I mean, you could do that but its a lot more code than what is necessary.  All you have to do is delay the command long enough so that the combobox receives the Enter key and reacts to it.  The Dispatcher can give you that delay.  Be sure to remove the e.Handled part.

Reply Children