在虚拟化TreeView中滚动问题

use*_*578 9 c# wpf treeview scroll

今天我决定最终尝试虚拟化TreeView.要做到这一点,需要绑定.所以我决定进行2项测试 - 基于类型+虚拟化的HierarchicalDataTemplate.

我为一些数据创建了一个基类.从基类创建了2个派生类.制作2个HierarchicalDataTemplate(每个派生类1个)以获得不同的节点格式.并运行2种类型的10k节点的人口.

类别:

public class ListItem_Generic
{
    public string Name { get; protected set; }
    public ListItem_Generic(string Name = "") { this.Name = Name; }
}

public class ListItem_Single : ListItem_Generic
{
    public ListItem_Single(string Name = "") : base(Name) { }
}

public class ListItem_Multi : ListItem_Generic
{
    public List<ListItem_Generic> Items { get; protected set; }
    public ListItem_Multi(string Name = "", List<ListItem_Generic> Items = null)
        : base(Name)
    {
        if (Items == null) 
            this.Items = new List<ListItem_Generic>(); 
        else 
            this.Items = new List<ListItem_Generic>(Items);
    }
}
Run Code Online (Sandbox Code Playgroud)

生成带有一些子节点的10k第一级节点,绑定:

    public MainWindow()
    {
        InitializeComponent();

        // Create a list of sample items and populate them
        var lst = new List<ListItem_Generic>();

        int MaxHeaders = 10000;
        var rnd = new Random();
        // Now generate 10 000 records. First select random amount of headers
        int HeadersCount = rnd.Next(MaxHeaders);

        for (int i = 0; i < HeadersCount; i++)
        {
            var Childrencount = rnd.Next(100);
            var children = new List<ListItem_Generic>();
            for (int j = 0; j < Childrencount; j++)
                children.Add(new ListItem_Single("Child #"+j+" of parent #"+i));
            lst.Add(new ListItem_Multi("Header #" + i + " (" + Childrencount + ")", children));
        }
        for (int i = 0; i < MaxHeaders - HeadersCount; i++)
            lst.Add(new ListItem_Single("Line #" + i));

        // Bind lstView to lst
        lstView.ItemsSource = lst;
        lstView.UpdateLayout();
    }
Run Code Online (Sandbox Code Playgroud)

带有HierarchicalDataTemplates的XAML:

<Window x:Class="Test_DataTemplates.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:loc="clr-namespace:Test_DataTemplates"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TreeView Name="lstView" VirtualizingPanel.IsVirtualizing="True">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type loc:ListItem_Multi}" ItemsSource="{Binding Path=Items}">
                    <Border Background="RosyBrown">
                        <TextBlock Text="{Binding Path=Name}" Foreground="White" FontWeight="Bold"/>
                    </Border>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type loc:ListItem_Single}">
                    <TextBlock Text="{Binding Path=Name}"/>
                </HierarchicalDataTemplate>
            </TreeView.Resources>
        </TreeView>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

一切都很好:

  • treeview变得虚拟化(通过内存占用和加载时间很容易注意到)
  • 从类型派生的节点格式正确

然而,当滚动让我们说标题#1000并扩展它时 - 滚动位置会跳转到其他地方,使得展开的节点及其子节点不可见.

我做错了什么?有没有什么办法解决这一问题?

更新:删除虚拟化也会删除滚动错误.

pap*_*zzo 0

尝试关闭(而不是打开)回收

VirtualizingStackPanel.VirtualizationMode="Standard"
Run Code Online (Sandbox Code Playgroud)

优化文章提到开启

如何:提高 TreeView 的性能