虚拟化ItemsControl?我有一个ItemsControl但是,它包含了我想要虚拟化的数据列表。VirtualizingStackPanel.IsVirtualizing="True"似乎不适用于ItemsControl.这是真的吗?还是有其他我不知道的方法?为了进行测试,我使用了以下代码块:<ItemsControl ItemsSource="{Binding Path=AccountViews.Tables[0]}"
VirtualizingStackPanel.IsVirtualizing="True"><ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Initialized="TextBlock_Initialized"
Margin="5,50,5,50" Text="{Binding Path=Name}" />
</DataTemplate></ItemsControl.ItemTemplate></ItemsControl>如果我更改ItemsControl转到ListBox,我可以看到Initialized事件只运行几次(巨大的边距,所以我只需要看几个记录),但是作为一个ItemsControl每个项目都会被初始化。我试过设置ItemsControlPanelTemplate转到VirtualizingStackPanel但这似乎没什么用。
3 回答
守候你守候我
TA贡献1802条经验 获得超10个赞
其实不仅仅是ItemsPanelTemplate
使用VirtualizingStackPanel
..默认ControlTemplate
为ItemsControl
没有ScrollViewer
,这是虚拟化的关键。添加到默认控件模板中。ItemsControl
(使用控件模板ListBox
作为模板)给我们提供了以下内容:
<ItemsControl VirtualizingStackPanel.IsVirtualizing="True" ScrollViewer.CanContentScroll="True" ItemsSource="{Binding Path=AccountViews.Tables[0]}"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Initialized="TextBlock_Initialized" Text="{Binding Path=Name}" /> </DataTemplate> </ItemsControl.ItemTemplate> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.Template> <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> </ItemsControl.Template></ItemsControl>
慕雪6442864
TA贡献1812条经验 获得超5个赞
<!--Virtualised ItemsControl--><Style x:Key="ItemsControlVirtualizedStyle" TargetType="ItemsControl"> <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/> <Setter Property="ScrollViewer.CanContentScroll" Value="True"/> <Setter Property="ItemsPanel"> <Setter.Value> <ItemsPanelTemplate> <VirtualizingStackPanel /> </ItemsPanelTemplate> </Setter.Value> </Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ItemsControl"> <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></Style>
12345678_0001
TA贡献1802条经验 获得超5个赞
ItemsPanel
VirtualizingStackPanel
<ItemsControl> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel></ItemsControl>
添加回答
举报
0/150
提交
取消