WPF中DataGridCell编辑模式问题

3

问题如下:

当我们想要编辑一个 DataGridCell 时,我们首先需要选择该行的 DataGridRow。也就是说,如果要编辑 DataGridRow.Current 以外的 DataGridCell,我们需要执行以下两个步骤: 1. 单击单元格以选择行,2. 双击单元格进入编辑模式。我的问题是:有没有可能只用一次单击就进入编辑模式?

1个回答

1
首先,您可以设置SelectionUnit="Cell",只选择一个单元格。
其次,您可以使用键盘上的F2键开始编辑。
要在单击单元格时开始编辑,需要添加以下事件处理程序GotFocus:
<DataGrid Name="MyDataGrid" SelectionUnit="Cell" GotFocus="MyDataGrid_GotFocus" ...>

代码后台

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);
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接