0

I'm using a WPF TreeView with virtualization enabled:

VirtualizingStackPanel.IsVirtualizing="True"

VirtualizingStackPanel.VirtualizationMode="Recycling"

I would like to select an item which is not visible. E.g. having a tree like this

0 
1 
3 --
   - 3.1
   - 3.2
   - ...
   - 3.500
   - ...
   - 3.1000
4 
5

I would like to select entry 3.500. I've read a lot of other answers already but I got nothing to work. The only thing that's working is this:

VirtualizingPanel panel = VisualHelper.FindVisualChild<VirtualizingPanel>( this.treeView );
  
panel.BringIndexIntoViewPublic( indexOfItem );

Unfortunately this only seems to work with non hierarchical controls. In my example above the max index is 5 which scrolls to the end of the list (making Item "5" visible).
But what if I want to scroll to an item which is in the child of another item? Like item 3.500? BringIndexIntoViewPublic is not working anymore.
Calls to someTreeViewItem.BringIntoView() are also not working - I guess because the items container was not build as its off-screen.
Is there another way to use VirtualizingStackPanel and programmatically select an item which is not in view?

zpete
  • 1,725
  • 2
  • 18
  • 31

1 Answers1

-1

You need to bring the item to be selected into view before you can actually select it.

There is a tutorial in the official docs that explains how to find a TreeViewItem in a virtualized TreeView:

How to: Find a TreeViewItem in a TreeView

You need to expand the container:

if (container is TreeViewItem && !((TreeViewItem)container).IsExpanded)
{
    container.SetValue(TreeViewItem.IsExpandedProperty, true);
}

...and then try to generate the ItemsPresenter and the ItemsPanel by calling ApplyTemplate:

container.ApplyTemplate();
ItemsPresenter itemsPresenter =
    (ItemsPresenter)container.Template.FindName("ItemsHost", container);
if (itemsPresenter != null)
{
    itemsPresenter.ApplyTemplate();
}
...
mm8
  • 163,881
  • 10
  • 57
  • 88