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?