none
Virtualizing TreeViewItem RRS feed

  • Question

  • I've replaced the usual ItemsPanelTemplate with a VirtualizingStackPanel in my TreeView and TreeViewItems. Everything is working in my TreeView, but I'm not getting the desired virtual behavior in my TreeViewItems.

    I've also notice that my ScrollViewer's thumb size is getting messed up whenever there are expanded TreeViewItems. Scrolling them into and out of the view changes the thumbsize. I presume this is because the TreeViewItem's VirtualizingStackPanel does not know who its ScrollOwner is.

     

    Here's some code:

     

    <ControlTemplate x:Key="treetemplate" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

    <Border

    BorderBrush="{TemplateBinding Border.BorderBrush}"

    BorderThickness="{TemplateBinding Border.BorderThickness}"

    SnapsToDevicePixels="True">

    <ScrollViewer

    Background="{TemplateBinding Panel.Background}"

    HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"

    VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"

    SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"

    Focusable="False"

    Padding="{TemplateBinding Control.Padding}"

    CanContentScroll="True">

    <ItemsPresenter />

    </ScrollViewer>

    </Border>

    </ControlTemplate>

    <ItemsPanelTemplate x:Key="virtualize">

    <VirtualizingStackPanel

    VirtualizingStackPanel.IsVirtualizing="True"

    IsItemsHost="True"

    Background="Azure"/>

    </ItemsPanelTemplate>

     

    class ItemContent : Object

    {

    public String _content;

    public ItemContent(String content) { _content = content; }

    public override string ToString()

    {

    Console.WriteLine(_content);

    return _content;

    }

    }

     

    public Window1()

    {

    InitializeComponent();

    TreeView view = new TreeView();

    ControlTemplate template = FindResource("treetemplate") as ControlTemplate;

    view.Template = template;

    ItemsPanelTemplate panelTemplate = FindResource("virtualize") as ItemsPanelTemplate;

    view.ItemsPanel = panelTemplate;

    int n = 100;

    createItems(view, n, "root", panelTemplate);

    for (int i = 0; i < n; i++)

    {

    TreeViewItem item = view.ItemsIdea as TreeViewItem;

    createItems(item, n, "child", panelTemplate);

    }

    container.Children.Add(view);

    Canvas.SetTop(view, 5);

    Canvas.SetLeft(view, 5);

    view.Height = 200;

    view.Width = 200;

    MouseDown += delegate(Object sender, MouseButtonEventArgs args)

    {

    dumpVisualTreeChildren(view, 0);

    };

    }

     

    void createItems(ItemsControl parent, int count, String label, ItemsPanelTemplate template)

    {

    for (int i = 0; i < count; i++)

    {

    TreeViewItem item = new TreeViewItem();

    item.Header = new ItemContent(label + " : " + i);

    item.ItemsPanel = template;

    item.Loaded += ItemLoaded;

    parent.Items.Add(item);

    }

    }

     

     

    Any Suggestions?

    Tuesday, March 27, 2007 2:19 PM

Answers

  • VirtualizingStackPanel and the virtualizing feature only work on one level. So, you might be able to get it to work on TreeView, but it does not work with TreeViewItems. VSP relies on communication with the ScrollViewer, which only occurs at the top level. If you need virtualization using a tree UI, you might consider using a list control and insert/remove as tree items are expanded/collapsed. List-type controls, such as ListBox or ListView, work with VSP.

     

    Ben

    Tuesday, April 3, 2007 9:11 PM

All replies

  • Can one ScrollViewer be the ScrollOwner to many TreeViewItems? Would that help?
    Tuesday, March 27, 2007 8:12 PM
  • VirtualizingStackPanel and the virtualizing feature only work on one level. So, you might be able to get it to work on TreeView, but it does not work with TreeViewItems. VSP relies on communication with the ScrollViewer, which only occurs at the top level. If you need virtualization using a tree UI, you might consider using a list control and insert/remove as tree items are expanded/collapsed. List-type controls, such as ListBox or ListView, work with VSP.

     

    Ben

    Tuesday, April 3, 2007 9:11 PM
  • Ben, I guess that's an option...

    It's unfortunate that if you want anything more than the simplest tree (ie multi-select, columns, virtual behavior...) you have to roll your own from a ListView. Sort of feels like TreeView should have been implemented this way from the start...

    Thursday, April 5, 2007 2:17 PM
  • Having a TreeView/ListView combination is a request we have been seeing since the release, and something we have on our TODO list.

     

    Ben

    Thursday, April 5, 2007 5:59 PM
  • That's good news. Thank you for the update.
    Thursday, April 5, 2007 7:12 PM