WPF TreeView 中的键盘导航
我的 TreeViewItem.Items 数据模板包含 2 个文本框。当我在第一个文本框中按 Tab 键时,焦点将转到第二个文本框。我想当我在第二个文本框上按 Tab 时 - 焦点将放在下一个 TreeViewItem 子项上的第一个文本框中,如果 TreeViewItem 没有下一个子项,则焦点将放在下一个 TreeViewItem 上的第一个子项上。怎么做呢?
<TreeView Name="resultsTv"
ItemTemplate="{StaticResource excerciseResultDataTemplate}"
KeyboardNavigation.TabNavigation="Contained">
<TreeView.ItemContainerStyle>
<Style>
<Setter Property="TreeViewItem.IsExpanded" Value="True"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="Contained"></Setter>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
<HierarchicalDataTemplate x:Key="excerciseResultDataTemplate" ItemTemplate="{StaticResource setDataTemplate}" ItemsSource="{Binding Sets}">
<StackPanel Orientation="Horizontal" KeyboardNavigation.TabNavigation="Continue">
<Label Content="{Binding Name}"></Label>
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate x:Key="setDataTemplate">
<StackPanel Orientation="Horizontal" KeyboardNavigation.TabNavigation="Continue">
<TextBox Width="50" Text="{Binding Weight}"/>
<TextBox Width="50" Text="{Binding Repeats"/>
</StackPanel>
</DataTemplate>
My TreeViewItem.Items data template contains 2 textboxes. When I press Tab key in first textbox, focus goes on second textbox. I want to when I press Tab on second textbox - focus going on first textbox on next TreeViewItem subitem and if there are TreeViewItem has not next subitem, focus goes on first subitem on next TreeViewItem. How to do that?
<TreeView Name="resultsTv"
ItemTemplate="{StaticResource excerciseResultDataTemplate}"
KeyboardNavigation.TabNavigation="Contained">
<TreeView.ItemContainerStyle>
<Style>
<Setter Property="TreeViewItem.IsExpanded" Value="True"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="Contained"></Setter>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
<HierarchicalDataTemplate x:Key="excerciseResultDataTemplate" ItemTemplate="{StaticResource setDataTemplate}" ItemsSource="{Binding Sets}">
<StackPanel Orientation="Horizontal" KeyboardNavigation.TabNavigation="Continue">
<Label Content="{Binding Name}"></Label>
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate x:Key="setDataTemplate">
<StackPanel Orientation="Horizontal" KeyboardNavigation.TabNavigation="Continue">
<TextBox Width="50" Text="{Binding Weight}"/>
<TextBox Width="50" Text="{Binding Repeats"/>
</StackPanel>
</DataTemplate>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己也遇到了这个问题,我在互联网上查找了这个问题,只看到了有关 WPF 商业自定义控件的论坛帖子。但是,由于它们依赖于 WPF 的通用容器设计,因此它们仍然可以工作:
将
TreeView
标记的KeyboardNavigation.TabNavigation
属性设置为Contained
并在您的树中包括以下内容:这存在三个问题。 Shift-Tab 根本不起作用。 (请参阅此问题。)此外,向上和向下箭头键不没有做太多事情。我刚刚发现这样做将处理 MouseLeftButtonUp 事件,这样您自己的事件就不会被触发。
Having this problem myself I looked this up on the internet and only saw forum posts about commercial custom controls for WPF. However, since they rely on the generic container design of WPF they still work:
Set the
KeyboardNavigation.TabNavigation
property of yourTreeView
tag toContained
and include the following in your tree:There are three problems with this. Shift-tab simply doesn't work. (See this question.) Additionally, the up and down arrow keys don't do much. And I just discovered doing this will handle the MouseLeftButtonUp event so that your own event won't be triggered.