Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrolling Issue in Virtualized TreeView

Today I decided to finally try virtualized TreeView. To do that binding is required. So I decided to get 2 things test - HierarchicalDataTemplate based on types + virtualization.

I created a base class for some data. Created 2 derived classes from base class. Made 2 HierarchicalDataTemplate (1 for each derived class) to get different formatting of nodes. And running population of 10k nodes of 2 types.

Classes:

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);
    }
}

Generation of 10k 1st level nodes with some children, binding:

    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();
    }

XAML with HierarchicalDataTemplates:

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

Everything works finely:

  • treeview gets virtualized (easily noticeable by memory footprint + loading times)
  • Nodes derived from types are correctly formatted

Yet, when scrolling to let's say header #1000 and expanding it - scroll position would jump to some other place making expanded node and its children NOT visible.

What did I do wrong? Is there any way to fix this?

Update: Removing virtualization also removes scrolling bug.

like image 665
user2274578 Avatar asked Apr 12 '13 13:04

user2274578


1 Answers

After having many issues with Tree virtualization in C# WPF (including major issue, that only the very first level gets virtualized) - I was not able to find a proper fix. Microsoft accepted a bug report and answered, that scrolling issue will get fixed in one of future releases.

As to the final solution to this for me personally - I've switched to own implementation of ListTreeView, i.e. using List and simulating tree. This solved all the issues with virtualization and with scrolling behavior. The only issue was - removal of many items after collapsing tree node. I had to implement a check if it's easier/faster to just recreate a fresh list instead of deleting items 1 by 1.

like image 131
user2274578 Avatar answered Nov 13 '22 17:11

user2274578