views:

54

answers:

1

I have the following ListBox:

            <ScrollViewer>
                <!--Spec Definitions-->
                <ListBox DataContext="{Binding SpecPackageSpecGroupListViewModel}" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" ScrollViewer.IsDeferredScrollingEnabled="True"
                     ItemContainerStyle="{StaticResource SpecPackageSpecGroupListBoxStyle}" 
                     ItemsSource="{Binding SortedChildren}" 
                     Background="Transparent"
                     BorderThickness="0" SelectionMode="Extended"
                     Margin="5,5,5,5">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Controls:SpecPackageSpecGroupControl/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </ScrollViewer>

This list box supposes to host ~1000 items, but complex ones. And i want it to work with the VirtualizingStackPanel, so i have set the visualizing xaml configuration to: VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling"

My problem is that i think it doesn't work - first - it takes extremely lots of time to load ~700 items - second when i break point on my control constructor - i can see it is called 700 times, check this out:

    public static int Counter = 0;

    public SpecPackageSpecGroupControl()
    {
        InitializeComponent();

        Counter++;
        if (Counter%100 == 0)
            Console.WriteLine("Hi");
    }

I break point on the Console.WriteLine("Hi") and i can see that the static counter reached to 700. So basically the UIElements are being created although this is a virtual mode.

Maybe i'm miss understanding the virtualization mode - or maybe (hopefully :-)) i'm just doing something wrong.

Your help will be extremely appreciated.

Thanx in advance, Gili

+1  A: 

Don't put it in a ScrollViewer. The XAML as you pasted indeed bypasses virtualization but for a different reason: the ListBox extends fully (without scrolling) because the scrollViewer allows it to. Because it is fully 'extended', the ListBox doesn't use virtualization. It will use its built-in scroll viewer if you place it in a regular container - Border, Grid etc.

Alex Paven