我编写了一个实现虚拟化的自定义面板。当放置在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>控件直接放置在窗口中。为什么VirtualizingStackPanel不能工作?
发布于 2014-03-14 14:21:09
获得你的第三个不工作的例子。我在我自己的一个项目中使用了这种风格,而且它正在起作用。
<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>
我看不出有什么很大的不同,但也许会对你有帮助。
https://stackoverflow.com/questions/22405471
复制相似问题