4

I'm using a virtualized treeview in WPF to display a 3 level deep hierarchy with a larger number of child nodes (5000+).

<TreeView Grid.Row="0" Grid.Column="0" Name="TestTree"  ItemsSource="{Binding}" VirtualizingStackPanel.IsVirtualizing="True">
...
</TreeView>

What happens is that if I expand the child nodes on the 3rd level and scroll to the very end the content doesn't display correctly and the scroller "flips back" and I can continue to scroll down forever, the behavior repeating after some more scrolling.

This only happens in virtualized mode, but unfortunately due to the large number of child nodes the loading time of the treeview in non-virtualized mode is prohibitive.

I have read the following links on SO here and the original thread on the MSDN forums here but the suggested workaround of calling UpdateLayout() in the SelectedItemChanged handler did not work for me.

Did anyone else experience this strange behavior before, and if so is there a workaround?

Appreciate any input - thanks!

Community
  • 1
  • 1
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335

1 Answers1

0

have you tryed to load the items lazy?? What i mean is that you could load at first (without virtualizing) only the root nodes, and then when each one of those nodes are expanded load it's childs.

I usually use a TreeViewItemViewModelClass in this cases, something like:

public class TreeViewItemViewModel : INotifyPropertyChanged
{
    public IEnumerable<TreeViewItemViewModel> Childs { get; }
    public bool IsSelected { get; set; }
    public bool IsExpanded { get; set; }
    (...)
}

and then in the ItemContainerStyle of the TreeView with a TwoWay binding bind the IsSelected and the IsExpanded propertires, and then in the setter of the property IsExpanded you load all it's childs.

I have tested this approach, with trees that in theory have more than 5000 items but never with all the items loaded at the same time.

Hope this helps...

Ariel
  • 1,641
  • 1
  • 18
  • 27
  • The problem is that most of the nodes are on the same level, so this optimization wouldn't really buy me anything since I would have to load all those nodes at the same time anyway. – BrokenGlass Jun 02 '11 at 19:45