Changing selection in a TreeView can be challenging. With the introduction of virtualization in TreeView in .NET 3.5 SP1, it can be even more challenging. This sample demonstrates a way to change selection in a TreeView that has virtualization enabled.

This project can be built and run using Microsoft Visual Studio 2008 SP1.

What is shown
  • How to enable virtualization on TreeView.
  • How to expose the BringIntoView API on VirtualizingStackPanel.
  • How to scroll each level into view on the way to the desired selected item.

What is not shown
  • The code relies on scrolling items into view and accessing the IsSelected property on the item. It does not provide a way to do so using only the data model.
  • It works around but does not fix the continually adjusting scrolling extent behavior. As items are scrolled into view, their actual sizes are calculated and the total scrolling extent is updated. This can cause the ScrollBar thumb to change size dramatically.

If you decide to use any of the code, please be sure to read any commented notes.