首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VirtualizingStackPanel拒绝虚拟化

VirtualizingStackPanel拒绝虚拟化
EN

Stack Overflow用户
提问于 2014-03-14 12:48:28
回答 1查看 1.2K关注 0票数 0

我编写了一个实现虚拟化的自定义面板。当放置在ListBox中时,一切都很好。

但是,如果我移除面板并使用默认的VirtualizingStackPanel (无论是在ListBox中还是在ItemsControl中重新开发以支持虚拟化),则控件不会虚拟化。

虚拟化确实起作用的一个例子:

代码语言:javascript
复制
<ListBox ItemsSource="{Binding Items}" ScrollViewer.CanContentScroll="True">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <CustomVirtualizingPanel />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

虚拟化不起作用的例子:

代码语言:javascript
复制
<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不能工作?

EN

回答 1

Stack Overflow用户

发布于 2014-03-14 14:21:09

获得你的第三个不工作的例子。我在我自己的一个项目中使用了这种风格,而且它正在起作用。

代码语言:javascript
复制
<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>

我看不出有什么很大的不同,但也许会对你有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22405471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档