22

我正在使用 Microsoft WPF 数据网格。我注意到 WPF 数据网格 DataGridTemplateColumn 的奇怪行为。当您在网格中使用 templateColumn 并且模板列包含一些控件时,当您从前一列中选择时,焦点不会自动赋予模板列中声明的第一个元素。焦点最初设置在模板列的边框上,当我们再次进行选项卡时,焦点转到第一列。此问题的任何解决方法。当我关闭标签时,如何将焦点设置为数据网格模板列中的第一个元素。

4

4 回答 4

12

我们通过修改 DataGridCell 上的样式解决了这个问题:

<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
    <Setter Property="IsTabStop" Value="False"/>
于 2013-11-08T15:34:50.243 回答
7

我通过处理网格的 PrepareCellForEdit 事件解决了这个问题。这是代码

void HODataGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
      UIElement inputElement;
      ///
      /// Texbox is the first control in my template column
      ///
      inputElement = HODataGridHelper.GetVisualChild<TextBox>(e.EditingElement);
      if (inputElement != null)
      {
           Keyboard.Focus(inputElement);
      }
}
于 2010-05-14T15:42:38.573 回答
2

有一个使用静态类的解决方案,并对 Xaml 进行一次更改以获取您想要关注的控件。“ WPF DataGrid:从一个单元格切换到另一个单元格不会将焦点设置在控件上

于 2010-05-27T17:09:53.110 回答
1

我在 WPF datagrid codeplex 讨论中找到了一个链接 http://www.codeplex.com/wpf/Thread/View.aspx?ThreadId=35540

感谢文森特西巴尔

于 2009-04-14T02:31:14.277 回答