views:

14

answers:

0

So I'm attempting to use the WPF TreeView in its virtualization mode (Visual Studio 2010, NET 4.0). Having done a bit of research, I found a variety of settings and configurations were needed to get this to work, or so various people at MSDN claimed.

However, having used Snoop on the same ObservableCollection (1000 items) when thrown at a ListBox and a Treeview (XAML below), I noticed something odd. Both UIElements claim they are virtualizing.

ListBox's VirtualizingStackPanel would happily have an ActualHeight of something reasonable (around 85) along with a RenderHeight to match, a small ViewportHeight, and various other things that suggested the listbox was indeed virtualizing.

TreeView's VirtualizingStackPanel on the other hand gets an ActualHeight of 15000 or so, implying the StackPanel has just grown and grown to fit the available data; also, contrasting with the listbox, if I traced out access to the collection, the listbox queried the first few elements; the treeview would repeatedly access the entire collection.

My first question would be why, and the followup would be that if TreeView isn't virtualizing, how do I get it to do so? I've tried just using a TreeView, just using the IsVirtualizing property on the TreeView; nothing seems to help. I even stepped through the VirtualizingStackPanel MeasureOverride code to try and find out what it's doing with no success.

Associated XAML:

<UserControl x:Class="TestWpfApplication.View.VirtualisedTree"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="20"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.Resources>
        <Style x:Key="TreeViewStyle">
            <Setter Property="TreeView.Background" Value="Transparent"/>
            <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
            <Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling"/>
            <Setter Property="TreeView.OverridesDefaultStyle" Value="True" />
            <Setter Property="ItemsControl.ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel IsItemsHost="True"
                                                Orientation="Vertical"/>
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="TreeView.Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TreeView">
                        <ScrollViewer Focusable="False"
                                      CanContentScroll="True"
                                      Padding="4"
                                      VerticalScrollBarVisibility="Auto">
                            <ItemsPresenter HorizontalAlignment="Stretch"/>
                        </ScrollViewer>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
    <Button Grid.Row="0" Command="{Binding DelegateLoad}" >Load</Button>
    <ListBox Grid.Row="1"
             ItemsSource="{Binding Path=Children}"
             DataContext="{Binding Root}">
    </ListBox>
    <TreeView Grid.Row="2"
              ItemsSource="{Binding Path=Children}"
              DataContext="{Binding Root}"
              Style="{StaticResource TreeViewStyle}" >
    </TreeView>
</Grid>

Thanks in advance for any help!