When you set VirtualizingStackPanel.IsVirtualizing="True"
on a TreeView
in WPF:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TreeView Name="MainTree" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling"/>
</Grid>
</Window>
The following code-behind with a call to TreeViewItem.BringIntoView()
does not work:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
TreeViewItem lastTvi2 = null;
for (int i = 0; i < 100; i++)
{
TreeViewItem tvi = new TreeViewItem();
tvi.Header = "Hello";
MainTree.Items.Add(tvi);
for (int j = 0; j < 100; j++)
{
TreeViewItem tvi2 = new TreeViewItem();
tvi2.Header = "World";
tvi.Items.Add(tvi2);
lastTvi2 = tvi2;
}
}
lastTvi2.BringIntoView();
}
}
}
Yet when you remove the line VirtualizingStackPanel.IsVirtualizing="True"
, it will bring the lastTvi2
object (last item in the tree) into the view. Why does this break the TreeViewItem.BringIntoView()
method? Is it normal behavior, or is it a .NET bug?