Hello,
When an XamDataGrid cell in edit mode, may I press the Up or Down key to move the edit mode to the adjacent cell? If so, are there any examples?
Thanks.
Here is one way to achieve this by handling the PreviewKeyDown event of the XamDataGrid :
void xamDataGrid1_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (!xamDataGrid1.ActiveCell.IsInEditMode)
return;
}
if (e.Key == Key.Left)
xamDataGrid1.ExecuteCommand(DataPresenterCommands.EndEditModeAndAcceptChanges);
xamDataGrid1.ExecuteCommand(DataPresenterCommands.CellLeft);
xamDataGrid1.ExecuteCommand(DataPresenterCommands.StartEditMode);
if (e.Key == Key.Right)
xamDataGrid1.ExecuteCommand(DataPresenterCommands.CellRight);
if (e.Key == Key.Up)
xamDataGrid1.ExecuteCommand(DataPresenterCommands.CellAbove);
if (e.Key == Key.Down)
xamDataGrid1.ExecuteCommand(DataPresenterCommands.CellBelow);
I'm using XamDataGrid Version 12.2 and tryin' to achieve the same as above but your solution doesn't seem to work for me. There are two problems:
1) I have three Columns A, B, C with Fieldsettings.AllowEdit of B set to false. If I use right arrow to move from cell A to B, B is automatically skipped and focus is on cell C but it's not in edit mode because PreviewKeyDown-Event wasn't fired.
2) Cell value isn't selected in edit mode
You can use the following code:
private void xamDataGrid1_PreviewKeyDown(object sender, KeyEventArgs e) { if (!xamDataGrid1.ActiveCell.IsInEditMode) { return; } if (e.Key == Key.Left) { xamDataGrid1.ExecuteCommand(DataPresenterCommands.EndEditModeAndAcceptChanges); do { xamDataGrid1.ExecuteCommand(DataPresenterCommands.CellLeft); } while (xamDataGrid1.ActiveCell.Field.Settings.AllowEdit == false); xamDataGrid1.ExecuteCommand(DataPresenterCommands.StartEditMode); } if (e.Key == Key.Right) { xamDataGrid1.ExecuteCommand(DataPresenterCommands.EndEditModeAndAcceptChanges); do { xamDataGrid1.ExecuteCommand(DataPresenterCommands.CellRight); } while (xamDataGrid1.ActiveCell.Field.Settings.AllowEdit == false); xamDataGrid1.ExecuteCommand(DataPresenterCommands.StartEditMode); } if (e.Key == Key.Up) { xamDataGrid1.ExecuteCommand(DataPresenterCommands.EndEditModeAndAcceptChanges); do { xamDataGrid1.ExecuteCommand(DataPresenterCommands.CellAbove); } while (xamDataGrid1.ActiveCell.Field.Settings.AllowEdit == false); xamDataGrid1.ExecuteCommand(DataPresenterCommands.StartEditMode); } if (e.Key == Key.Down) { xamDataGrid1.ExecuteCommand(DataPresenterCommands.EndEditModeAndAcceptChanges); do { xamDataGrid1.ExecuteCommand(DataPresenterCommands.CellBelow); } while (xamDataGrid1.ActiveCell.Field.Settings.AllowEdit == false); xamDataGrid1.ExecuteCommand(DataPresenterCommands.StartEditMode); } }
Hope this helps you.
Works nearly perfect. But my problem No. 2 is not solved. The values don't get selected after getting focus.
I can suggest you handle the XamDataGrid’s EditModeStarted event and add the following handler for it:
private void xamDataGrid1_EditModeStarted(object sender, Infragistics.Windows.DataPresenter.Events.EditModeStartedEventArgs e) { Dispatcher.BeginInvoke(new Action(() => { (Utilities.GetDescendantFromType(e.Editor as DependencyObject, typeof(TextBox), true) as TextBox).SelectAll(); }), System.Windows.Threading.DispatcherPriority.Background, null); }
Hello Soudip,
I have created a sample project for you where I implemented the same functionality for the XamGrid.
Hi,
Do we have similar functionality for XamGrid as we are using that.
Thanks
Soudip
Now it's perfect, thank you very much!