Pregunta

Here is the problem:

When we want to edit a DataGridCell, we have to select the DataGridRow at first. I mean for editing a DataGridCell outside the DataGridRow.Current, we need to 1. Click the cell to select row, 2. DoubleClick the cell to go to edit mode. My question is how can we go to edit mode just with a single click? Is it possible?

¿Fue útil?

Solución

First, you can set the SelectionUnit="Cell", was selected to only one Cell.

Second, you can start editing with key F2.

To start editing when you click on a Cell, you need to add the following event handler GotFocus:

<DataGrid Name="MyDataGrid" SelectionUnit="Cell" GotFocus="MyDataGrid_GotFocus" ...>

Code behind

private void MyDataGrid_GotFocus(object sender, RoutedEventArgs e)
{     
    if (e.OriginalSource.GetType() == typeof(DataGridCell))
    {            
        DataGrid MyDataGrid = (DataGrid)sender;

        if ((MyDataGrid != null) && (MyDataGrid.IsReadOnly == false))
        {
            MyDataGrid.BeginEdit(e);
        }
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top