树视图上的 VirtualizingStackPanel 不是虚拟化

发布于 2024-11-04 06:30:31 字数 1793 浏览 3 评论 0原文

我在这里遇到一个问题,我想在 TreeView 中显示一些项目,大约 100.000 个元素。 如果我使用默认的 WPF TreeView,一切似乎都可以工作,但如果我使用自定义 TreeView(目前只是一个 ItemsControl),虚拟化似乎不再工作。 在网上研究时,我尝试了一些解决方案,但似乎都不起作用...... 这是我的 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 中,所以这不应该是问题......但它是什么???

问候,

于尔根

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>

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

Greets,

Jürgen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虐人心 2024-11-11 06:30:31

VirtualizingStackPanel 有一些特殊的代码来查找 TreeView 和 TreeViewItem。此外,TreeViewItem 实现了 VirtualizingStackPanel.IProvideStackingSize,这是您无法实现的内部接口。

因此,如果您尝试复制 TreeView 中的项目的层次结构,那么您必须从 TreeView 派生才能使用虚拟化(而不是 ItemsControl)。

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.

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文