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!
Hi J_Feuerstein,
The original issue with the code that you had before was that when you had the comboeditor dropdown open with a value highlighted and you pressed the Enter key, it did not select the item first before moving on to the next cell. The reason it didn't select the value was because the PreviewKeyDown event was used and e.Handled was set to true. When you handle this event, it keeps the internal controls from receiving the event. This kept the comboeditor from selecting the item.
In order for the comboeditor to select the item it needs to handle the Enter key, so that's why e.Handled needed to be removed. This leads to the next issue: How do we move to the next cell after the Enter key has been pressed? This is where the Dispatcher comes in. The code inside the Dispatcher is not executed immediately so we can use this to delay when certain code is executed. Here is the new order of events thanks to the dispatcher:
1.) Enter key is pressed2.) PreviewKeyDown event is fired3.) Dispatcher code is sent off to be executed at a later time4.) Enter key signal is sent to XamComboEditor5.) XamComboEditor reacts and selects the highlighted item6.) Dispatcher code finally executes and tabs to next cell
Hopefully this helps explain it. Let me know if there is anything you are unclear about.
Hi Rob,
I've tried this out and it works like I want it.But I do not understand why it works, when I do not set e.Handled = true.In my understanding the default behavior of the grid should commit the record because the return input is sent to the grid?
Thanks!
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.