UWP WinUI TreeView 以编程方式滚动到项目

2024-02-13

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

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


目前,没有这样的APITreeView用于滚动到视图中的类。但你可以获得TreeViewList在树视图中ControlTemplate。并且它是基于ListViewBase其中包含ScrollIntoView方法。为了得到TreeViewList你可以用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。

<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>

Usage

private void Button_Click(object sender, RoutedEventArgs e)
{
    var listControl = FindChildByName(treeView1, "ListControl") as ListViewBase;
    listControl.ScrollIntoView(treeView1.RootNodes.LastOrDefault());
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UWP WinUI TreeView 以编程方式滚动到项目 的相关文章

随机推荐