亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

虛擬化ItemsControl?

虛擬化ItemsControl?

qq_笑_17 2019-06-23 16:44:30
虛擬化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..默認ControlTemplateItemsControl沒有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>


查看完整回答
反對 回復 2019-06-23
?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

在DavidN的回答的基礎上,您可以在ItemsControl上使用一種樣式來虛擬化它:

<!--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>

我不喜歡使用ListBox的建議,因為它們允許選擇不一定需要的行。


查看完整回答
反對 回復 2019-06-23
?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

只是默認的ItemsPanel不是VirtualizingStackPanel..你需要改變它:

<ItemsControl>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel></ItemsControl>


查看完整回答
反對 回復 2019-06-23
  • 3 回答
  • 0 關注
  • 721 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號