DataGridTemplateColumn 中的 WPF AutoCompleteBox 键盘导航问题
我在 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>
I'm using an AutoCompleteBox from the WPF Toolkit inside a DataGridTemplateColumn's editing template in WPF4. It works well enough for the most part once I sorted out all the niggling binding issues around DataGrid binding and also AutoCompleteBox's own gotchas and incompleteness. So far so good. The problem is keyboard navigation.
This is the scenario: there's a DataGrid with two columns. The first is a DataGridTemplateColumn which has an AutoCompleteBox in its editing template. The second is just an ordinary DataGridTextColumn.
If I invoke editing of a row, I can choose an item in the AutoCompleteBox. I press tab to move to the next column, but instead the row edit gets committed, and the keyboard focus doesn't move to the next column. If this was a DataGridTextColumn, it would stay in edit mode and let me edit the next column. This also happens for new rows.
To my mind it seems like there's something wrong with where WPF decides to send the keyboard focus when it comes out of the Autocompletebox, but I can't figure out what I can do about it, and I also haven't been able to find anybody talking about the same problem, which suggests either I'm doing something wrong or nobody else cares about keyboard navigation through their grids. I have been using a TemplateColumn subclass which overrides PrepareCellForEditing to ensure the focus lands in the AutoCompleteBox immediately on editing the cell (as per other answers here), but the problem persists if I disable all that code so it's not an effect of that bit of trickery as far as I can tell.
Any ideas?
The XAML looks more or less like this (simplified, of course, the grid has a lot more than two columns and some rather complicated data binding going on - I've left out the bindings and kept it to the overall structure).
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了将焦点转移到下一个专栏,我制作了扩展类(选项卡对我来说效果很好):
For moving focus to the next column I've made extended class (tab works fine for me):