1

我正在使用 WPFDataGrid 控件,并希望Cell通过单击将键盘焦点设置为选中。默认用户必须双击单元格才能开始编写。我尝试过的代码是:

<Style TargetType="{x:Type DataGridCell}" x:Key="DataGridCellStyle">
   <Setter Property="IsTabStop" Value="True" />
   <Setter Property="Focusable" Value="True" />    
   <Style.Triggers>
      <Trigger Property="IsKeyboardFocusWithin" Value="True">
        <Setter Property="IsEditing" Value="True" />
      </Trigger>
   </Style.Triggers>
</Style>

当我单击一个单元格时,它会到达Editmode,我需要再次单击以在此处设置键盘焦点并开始写作!

4

1 回答 1

2

我不确定这是否是一个不错的功能。您可能无法选择多个单元格。无论如何处理OnCurrentCellChanged事件

void DG1_OnCurrentCellChanged(object sender, SelectedCellsChangedEventArgs e)
{
    DataGrid dg=(Datagrid)sender;
    dg.BeginEdit();
}

BeginEdit()导致 DataGridPreparingCellForEdit 事件发生,我认为您应该处理该事件:

private void dg_PreparingCellForEdit(object sender,  DataGridPreparingCellForEditEventArgs e)
{
TextBox tb = e.EditingElement as TextBox;
if (tb != null)
   {
       tb.Focus();
       //you can set caret position and ...
   }
}

此外,您可以处理BeginningEdit 事件。

于 2012-12-15T12:38:12.273 回答