Каков ваш SelectionUnit для DataGrid?Это работает для SelectionUnit = "CellOrRowHeader", в противном случае некоторые вещи должны быть немного изменены в конце.В любом случае, следующий код переводит ячейку в режим редактирования, а затем фиксирует ее, которая вызывает событие CellEditEnding.
public void EnterCellEditEndingForCell(DataGridColumn column, DataRowView dataRowView)
{
int selectedRowIndex = -1;
// c_dataGrid.Items.IndexOf(dataRowView)
// sometimes break and return -1.
for (int i = 0; i < c_dataGrid.Items.Count; i++)
{
DataRowView rowView = c_dataGrid.Items[i] as DataRowView;
if (rowView == dataRowView)
{
selectedRowIndex = i;
break;
}
}
int selectedColumnIndex = c_dataGrid.Columns.IndexOf(column);
if (selectedRowIndex >= 0 && selectedColumnIndex >= 0)
{
DataGridCell dataGridCell = DataGridHelper.GetCell(c_dataGrid, selectedRowIndex, selectedColumnIndex);
if (dataGridCell == null)
{
c_dataGrid.ScrollIntoView(dataRowView);
dataGridCell = DataGridHelper.GetCell(c_dataGrid, selectedRowIndex, selectedColumnIndex);
}
dataGridCell.BringIntoView();
dataGridCell.Focus();
c_dataGrid.SelectedCells.Clear();
DataGridCellInfo info = new DataGridCellInfo(dataGridCell);
c_dataGrid.SelectedCells.Add(info);
c_dataGrid.BeginEdit();
c_dataGrid.CommitEdit(DataGridEditingUnit.Cell, true);
}
}
DataGridHelper.cs, если у вас нет реализации этого
static class DataGridHelper
{
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;
}
static public DataGridCell GetCell(DataGrid dg, int row, int column)
{
DataGridRow rowContainer = GetRow(dg, row);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
// try to get the cell but it may possibly be virtualized
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
{
// now try to bring into view and retreive the cell
dg.ScrollIntoView(rowContainer, dg.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
return null;
}
static public DataGridRow GetRow(DataGrid dg, int index)
{
DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
// may be virtualized, bring into view and try again
dg.ScrollIntoView(dg.Items[index]);
row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
}
ОБНОВЛЕНИЕ Если у вас выбрана только одна ячейка (что звучит как случай), то вы можете сделать это.
private void tb_Comments_KeyUp(object sender, KeyEventArgs e)
{
if (rowBeingEdited != null)
{
rowBeingEdited.Row["Comments"] = tb_Comments.Text;
DataGridColumn columnBeingEdited= GetActiveDataGridColumn();
EnterCellEditEndingForCell(columnBeingEdited, rowBeingEdited);
}
}
private DataGridColumn GetActiveDataGridColumn()
{
if (c_dataGrid.SelectedCells.Count != 1)
{
return null;
}
return c_dataGrid.SelectedCells[0].Column;
}