【问题标题】:WPF Datagrid -DataGridTemplateColumn tab focus issueWPF Datagrid -DataGridTemplateColumn 选项卡焦点问题
【发布时间】:2010-10-19 06:22:40
【问题描述】:

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

【问题讨论】:

标签: wpf datagrid datagridtemplatecolumn


【解决方案1】:

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

<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
    <Setter Property="IsTabStop" Value="False"/>

【讨论】:

  • 完美运行。谢谢!
【解决方案2】:

我通过处理网格的 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);
      }
}

【讨论】:

  • 什么是 HODataGridHelper?我尝试在 .NET 4 项目中使用此代码,但它不知道 HODataGridHelper 是什么,而且我在其他任何地方都找不到适用的 GetVisualChild 函数。
  • Pete,这是我们创建的自定义类。抱歉,我现在无法访问那段代码。它是框架 GetVisualChild 方法的变体:msdn.microsoft.com/en-us/library/…
  • 非常感谢。我搜寻了几个小时才找到这个。
【解决方案3】:

有一个使用静态类的解决方案,并对 Xaml 进行一次更改以获取您想要集中的控件。 "WPF DataGrid: Tabbing from cell to cell does not set focus on control"

【讨论】:

【解决方案4】:

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

感谢文森特·西巴尔

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    相关资源
    最近更新 更多