qq_笑_17
2019-06-23 16:44:30
虛擬化ItemsControl?我有一個(gè)ItemsControl但是,它包含了我想要虛擬化的數(shù)據(jù)列表。VirtualizingStackPanel.IsVirtualizing="True"似乎不適用于ItemsControl.這是真的嗎?還是有其他我不知道的方法?為了進(jìn)行測(cè)試,我使用了以下代碼塊:<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轉(zhuǎn)到ListBox,我可以看到Initialized事件只運(yùn)行幾次(巨大的邊距,所以我只需要看幾個(gè)記錄),但是作為一個(gè)ItemsControl每個(gè)項(xiàng)目都會(huì)被初始化。我試過(guò)設(shè)置ItemsControlPanelTemplate轉(zhuǎn)到VirtualizingStackPanel但這似乎沒(méi)什么用。
3 回答

守候你守候我
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊
其實(shí)不僅僅是ItemsPanelTemplate
使用VirtualizingStackPanel
..默認(rèn)ControlTemplate
為ItemsControl
沒(méi)有ScrollViewer
,這是虛擬化的關(guān)鍵。添加到默認(rèn)控件模板中。ItemsControl
(使用控件模板ListBox
作為模板)給我們提供了以下內(nèi)容:
<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貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊
<!--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貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
ItemsPanel
VirtualizingStackPanel
<ItemsControl> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel></ItemsControl>
添加回答
舉報(bào)
0/150
提交
取消