【问题标题】:WPF AutoCompleteBox in DataGridTemplateColumn keyboard navigation issuesDataGridTemplateColumn 中的 WPF AutoCompleteBox 键盘导航问题
【发布时间】:2011-10-19 10:52:44
【问题描述】:

我在 WPF4 中的 DataGridTemplateColumn 的编辑模板中使用 WPF 工具包中的 AutoCompleteBox。一旦我解决了围绕 DataGrid 绑定的所有琐碎绑定问题以及 AutoCompleteBox 自己的陷阱和不完整性,它在大多数情况下都运行良好。到现在为止还挺好。问题是键盘导航。

这是场景:有一个包含两列的 DataGrid。第一个是 DataGridTemplateColumn,它的编辑模板中有一个 AutoCompleteBox。第二个只是一个普通的DataGridTextColumn。

如果我调用行的编辑,我可以在 AutoCompleteBox 中选择一个项目。我按 Tab 键移动到下一列,但行编辑被提交,并且键盘焦点不会移动到下一列。如果这是一个 DataGridTextColumn,它将保持编辑模式并让我编辑下一列。新行也会发生这种情况。

在我看来,当 WPF 从自动完成框出来时,它决定发送键盘焦点的位置似乎有问题,但我不知道我能做些什么,而且我也没有能够找到任何人谈论同样的问题,这表明要么我做错了什么,要么没有其他人关心通过他们的网格进行键盘导航。我一直在使用 TemplateColumn 子类,它覆盖 PrepareCellForEditing 以确保焦点在编辑单元格时立即落在 AutoCompleteBox 中(根据此处的其他答案),但如果我禁用所有代码,问题仍然存在,所以它不是那一点的影响据我所知,诡计。

有什么想法吗?

XAML 看起来或多或少像这样(简化的,当然,网格有不止两列,还有一些相当复杂的数据绑定正在进行 - 我省略了绑定并将其保留在整体结构中) .

<DataGrid>
  <DataGridTemplateColumn Header="AutoCompleteBox">
    <DataGridTemplateColumn.CellTemplate>
      <DataTemplate><TextBlock /></DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
      <DataTemplate>
        <toolkit:AutoCompleteBox>
          <!-- autocompletebox's item template etc. -->
        </toolkit:AutoCompleteBox>
      </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
  </DataGridTemplateColumn>
  <DataGridTextColumn Header="Text" />
</DataGrid>

【问题讨论】:

    标签: wpf datagrid autocompletebox


    【解决方案1】:

    为了将焦点移到下一列,我制作了扩展类(标签对我来说很好用):

    public class ExAutoCompleteBox : AutoCompleteBox
    {
            public ExAutoCompleteBox()
            {
                PreviewKeyUp += (o, e) =>
                {
                    if (e.Key == Key.Enter)
                    {
                        ((UIElement)Keyboard.FocusedElement).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                    }
                };
            }
    }
    

    【讨论】:

    • 这正是我当时应该想到的,谢谢。事实证明,我们最终使用了 ItemsControl 和 ListBox 以及各种精心制作的 ItemTemplates,我们对此总体感到满意,但感谢您的回复!
    猜你喜欢
    • 2011-01-17
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 2011-09-05
    • 2012-10-29
    • 2011-04-25
    • 1970-01-01
    • 2019-05-27
    相关资源
    最近更新 更多