문제

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?

도움이 되었습니까?

해결책

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);
        }
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top