WPF TreeView 中的键盘导航

发布于 2024-11-12 00:25:12 字数 1524 浏览 4 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜宝宝 2024-11-19 00:25:12

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

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

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

这存在三个问题。 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 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>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文