WPF TreeView 中的键盘导航 [英] Keyboard navigation in WPF TreeView

查看:16
本文介绍了WPF TreeView 中的键盘导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 TreeViewItem.Items 数据模板包含 2 个文本框.当我在第一个文本框中按 Tab 键时,焦点转到第二个文本框.我想当我在第二个文本框上按 Tab 键时 - 焦点在下一个 TreeViewItem 子项上的第一个文本框上,如果 TreeViewItem 没有下一个子项,焦点在下一个 TreeViewItem 上的第一个子项上.该怎么做?

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>

推荐答案

我自己也遇到了这个问题,我在网上查了一下,只看到论坛上关于 WPF 的商业自定义控件的帖子.但是,由于它们依赖于 WPF 的通用容器设计,它们仍然可以工作:

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:

TreeView 标记的 KeyboardNavigation.TabNavigation 属性设置为 Contained 并在树中包含以下内容:

Set the KeyboardNavigation.TabNavigation property of your TreeView tag to Contained and include the following in your tree:

<TreeView.ItemContainerStyle>
    <Style TargetType="TreeViewItem">
        <Setter Property="KeyboardNavigation.TabNavigation" Value="Continue"/>
    </Style>
</TreeView.ItemContainerStyle>

这有三个问题.Shift-tab 根本不起作用.(请参阅此问题.)此外,向上和向下箭头键不'不做太多.而且我刚刚发现这样做会处理 MouseLeftButtonUp 事件,这样您自己的事件就不会被触发.

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.

这篇关于WPF TreeView 中的键盘导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆