我编写了一个实现虚拟化的自定义面板。当放置在 ListBox 中时,一切正常。
但是,如果我删除我的面板并使用默认的 VirtualizingStackPanel,在重新模板化以支持虚拟化的 ListBox 或 ItemsControl 中,控件不会虚拟化。
虚拟化确实有效的示例:
<ListBox ItemsSource="{Binding Items}" ScrollViewer.CanContentScroll="True">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<CustomVirtualizingPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
虚拟化不起作用的示例:
<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>
控件直接放置在 Window 内。为什么 VirtualizingStackPanel 不起作用?