Treeview上的VirtualizingStackPanel不能虚拟化 [英] VirtualizingStackPanel on a Treeview ist not Virtualizing

查看:119
本文介绍了Treeview上的VirtualizingStackPanel不能虚拟化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到问题,我想在TreeView中显示一些项目,大约100.000个元素. 如果我使用默认的WPF TreeView,一切似乎都可以正常工作,但是如果我使用自定义TreeView(目前仅是ItemsControl),则虚拟化似乎不再起作用. 在网络上进行研究时,我尝试了一些解决方案,但似乎都没有用…… 这是我的xaml:

I've got a problem here, I want to show some items in a TreeView, about 100.000 Elements. If i use the default WPF TreeView everything seems to work, but if i use a custom-TreeView (which is at the moment only an ItemsControl), virtualization doesn't seem to work anymore. While researching over the web, I've tried some solutions but none of them seems to work... Here's my xaml:

<Style TargetType="{x:Type my:MultiSelectionTreeView}">
    <Setter Property="TreeView.Background" Value="Transparent"/>
    <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
    <Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling"/>
    <Setter Property="TreeView.OverridesDefaultStyle" Value="True" />
    <Setter Property="ItemsControl.ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel IsItemsHost="True"/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="TreeView.Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type my:MultiSelectionTreeView}">
                <Border Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                    <ScrollViewer Focusable="True" CanContentScroll="true" 
                                  Padding="4" 
                                  VerticalScrollBarVisibility="Auto">
                        <ItemsPresenter HorizontalAlignment="Stretch"/>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这些项目位于具有DataBinding的ObservableCollection中,所以这不应该成为问题...但这是什么??

The Items are in an ObservableCollection with DataBinding, so that shouldn't be the Problem... but what is it???

问候,

于尔根

推荐答案

VirtualizingStackPanel具有一些用于查找TreeView和TreeViewItem的特殊代码.另外,TreeViewItem实现了VirtualizingStackPanel.IProvideStackingSize,这是您将无法实现的内部接口.

The VirtualizingStackPanel has some special code that looks for TreeView and TreeViewItem. In addition, TreeViewItem implements VirtualizingStackPanel.IProvideStackingSize, which is a internal interface which you won't be able to implement.

因此,如果您试图像TreeView中那样复制项目的层次结构,则必须从TreeView派生以使用虚拟化(而非ItemsControl).

So if you are trying to replicate the hierarchical structure of items like in the TreeView, then you'd have to derive from TreeView to use virtualization (not ItemsControl).

这篇关于Treeview上的VirtualizingStackPanel不能虚拟化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆