Thank you cooldadtx.
Referring to your answer, I added a keyup handler to the datagrid to create a code that saves the text being edited and moves the cell when the arrow key is pressed while editing.
This may not be the best workaround, but it still works. I attached my code below.
private void datagrid_eqp_KeyUp(object sender, KeyEventArgs e)
{
//key event
DataGrid dg = sender as DataGrid;
DataGridCellInfo cellinfo = dg.CurrentCell;
DataGridCell cell = TryToFindGridCell(dg, cellinfo);
var columnIndex = dg.Columns.IndexOf(dg.CurrentColumn);
var rowIndex = dg.Items.IndexOf(dg.CurrentItem);
if (cell.IsEditing == true && e.Key == Key.Right)
{
//cancel edit, save current text, move cell
cell.Content = "dummy"; //when press arrow, "dummy" deleted and current text is saved
this.datagrid_eqp.CancelEdit();
try
{
this.datagrid_eqp.CurrentCell = new DataGridCellInfo(datagrid_eqp.Items[rowIndex], datagrid_eqp.Columns[columnIndex + 1]);
}
catch (System.ArgumentOutOfRangeException)
{
//last column exception
this.datagrid_eqp.CurrentCell = new DataGridCellInfo(datagrid_eqp.Items[rowIndex], datagrid_eqp.Columns[columnIndex]);
}
}
else if (cell.IsEditing == true && e.Key == Key.Left)
{
//cancel edit, save current text, move cell
cell.Content = "dummy";
this.datagrid_eqp.CancelEdit();
try
{
this.datagrid_eqp.CurrentCell = new DataGridCellInfo(datagrid_eqp.Items[rowIndex], datagrid_eqp.Columns[columnIndex - 1]);
}
catch (System.ArgumentOutOfRangeException)
{
//first column exception
this.datagrid_eqp.CurrentCell = new DataGridCellInfo(datagrid_eqp.Items[rowIndex], datagrid_eqp.Columns[columnIndex]);
}
}
else if (cell.IsEditing == true && e.Key == Key.Up)
{
//cancel edit, save current text, move cell
cell.Content = "dummy";
this.datagrid_eqp.CancelEdit();
try
{
this.datagrid_eqp.CurrentCell = new DataGridCellInfo(datagrid_eqp.Items[rowIndex - 1], datagrid_eqp.Columns[columnIndex]);
}
catch (System.ArgumentOutOfRangeException)
{
//first row exception
this.datagrid_eqp.CurrentCell = new DataGridCellInfo(datagrid_eqp.Items[rowIndex], datagrid_eqp.Columns[columnIndex]);
}
}
else if (cell.IsEditing == true && e.Key == Key.Down)
{
//cancel edit, save current text, move cell
cell.Content = "dummy";
this.datagrid_eqp.CancelEdit();
try
{
this.datagrid_eqp.CurrentCell = new DataGridCellInfo(datagrid_eqp.Items[rowIndex + 1], datagrid_eqp.Columns[columnIndex]);
}
catch (System.ArgumentOutOfRangeException)
{
//last row exception
this.datagrid_eqp.CurrentCell = new DataGridCellInfo(datagrid_eqp.Items[rowIndex], datagrid_eqp.Columns[columnIndex]);
}
}
}
static DataGridCell TryToFindGridCell(DataGrid grid, DataGridCellInfo cellInfo)
{
DataGridCell result = null;
DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(cellInfo.Item);
if (row != null)
{
int columnIndex = grid.Columns.IndexOf(cellInfo.Column);
if (columnIndex > -1)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
result = presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex) as DataGridCell;
}
}
return result;
}
static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
}