虛擬化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
提交
取消