UWP WinUI TreeView 以编程方式滚动到项目 [英] UWP WinUI TreeView programatically scroll to item

查看:27
本文介绍了UWP WinUI TreeView 以编程方式滚动到项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新的 WinUI 工具包 TreeView 控件.我需要以编程方式滚动到特定项目.

I am trying to use new WinUI toolkit TreeView control. I need to scroll programmatically to specific item.

我找不到办法做到这一点.

I cannot find way to do this.

推荐答案

目前,TreeView 类中没有这样的 api 用于滚动到视图中.但是你可以在 TreeView ControlTemplate 中得到 TreeViewList.它基于包含 ScrollIntoView 方法的 ListViewBase.要获取 TreeViewList,您可以使用 VisualTreeHelper 类.

Currently, The is no such api in TreeView class that used to scroll into view. But you could get TreeViewList in TreeView ControlTemplate. And it is based on ListViewBase that contains ScrollIntoView method. For getting TreeViewList you could use VisualTreeHelper class.

public static DependencyObject FindChildByName(DependencyObject parant, string  ControlName)
{
    int count = VisualTreeHelper.GetChildrenCount(parant);

    for (int i = 0; i < count; i++)
    {
        var MyChild = VisualTreeHelper.GetChild(parant, i);
        if (MyChild is FrameworkElement && ((FrameworkElement)MyChild).Name == ControlName)
            return MyChild;

        var FindResult = FindChildByName(MyChild, ControlName);
        if (FindResult != null)
            return FindResult;
    }
    return null;
}

TreeViewList 名称是 TreeView 样式中的 ListControl.

And the TreeViewList name is ListControl in the TreeView style.

<TreeViewList x:Name="ListControl" AllowDrop="False" 
              CanReorderItems="False" 
              CanDragItems="False" 
              ItemContainerStyle="{StaticResource TreeViewItemStyle}" 
              ItemTemplate="{StaticResource CultureItemDataTemplate}">
    <TreeViewList.ItemContainerTransitions>
        <TransitionCollection>
            <ContentThemeTransition/>
            <ReorderThemeTransition/>
            <EntranceThemeTransition IsStaggeringEnabled="False"/>
        </TransitionCollection>
    </TreeViewList.ItemContainerTransitions> 
</TreeViewList>

使用

private void Button_Click(object sender, RoutedEventArgs e)
{
    var listControl = FindChildByName(treeView1, "ListControl") as ListViewBase;
    listControl.ScrollIntoView(treeView1.RootNodes.LastOrDefault());
}

这篇关于UWP WinUI TreeView 以编程方式滚动到项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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