0

I have written a custom panel which implements virtualization. When placed inside a ListBox, everything works fine.

However, if I remove my panel and use the default VirtualizingStackPanel, in either a ListBox or an ItemsControl re-templated to support virtualization, the control does not virtualize.

An example where virtualization does work:

<ListBox ItemsSource="{Binding Items}" ScrollViewer.CanContentScroll="True">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <CustomVirtualizingPanel />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

Examples where virtualization does not work:

<ListBox ItemsSource="{Binding Items}" VirtualizingStackPanel.IsVirtualizing="True"/>

<ListBox ItemsSource="{Binding Items}" ScrollViewer.CanContentScroll="True" VirtualizingStackPanel.IsVirtualizing="True">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel IsVirtualizing="True" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.Template>
        <ControlTemplate TargetType="{x:Type ItemsControl}">
            <Border BorderThickness="{TemplateBinding BorderThickness}"
                    Padding="{TemplateBinding Padding}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    Background="{TemplateBinding Background}"
                    SnapsToDevicePixels="True">
                <ScrollViewer CanContentScroll="True">
                    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                </ScrollViewer>
            </Border>
        </ControlTemplate>
    </ItemsControl.Template>
</ItemsControl>

The controls are placed directly inside a Window. Why does VirtualizingStackPanel not work?

Tom Gillen
  • 354
  • 4
  • 12
  • 1
    How are you validating `Virtualizing` is working or not? – Rohit Vats Mar 14 '14 at 13:30
  • @RohitVats The control takes about 10 seconds to load and a similar amount of time to attempt to scroll, and that time scales with the number of items contained within the items source. – Tom Gillen Mar 14 '14 at 13:56
  • It's important `where` are you placing these UI elements. For example, if you put this `ListBox` inside a `StackPanel` it's not going to virtualize. Post an example of the usage of these elements. – Federico Berasategui Mar 14 '14 at 14:58
  • @HighCore The `ListBox` is directly inside the `Window` with no other controls. – Tom Gillen Mar 14 '14 at 18:40

1 Answers1

0

Obtaining to your third non working example. I'm using this style in one of my own projects and it's working.

<Style x:Key="VirtualizedItemsControl"
   TargetType="{x:Type ItemsControl}">
<Setter Property="VirtualizingStackPanel.IsVirtualizing"
        Value="True" />
<Setter Property="ScrollViewer.CanContentScroll"
        Value="True" />
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate>
            <Border BorderThickness="{TemplateBinding Border.BorderThickness}"
                    Padding="{TemplateBinding Control.Padding}"
                    BorderBrush="{TemplateBinding Border.BorderBrush}"
                    Background="{TemplateBinding Panel.Background}"
                    SnapsToDevicePixels="True">
                <ScrollViewer Padding="{TemplateBinding Control.Padding}"
                              Focusable="False">
                    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                </ScrollViewer>
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>
<Setter Property="ItemsPanel">
    <Setter.Value>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </Setter.Value>
</Setter>

I can't see a huge difference but maybe it'll help you.

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51