WPF:TreeView 虚拟化不起作用

发布于 2024-10-04 08:54:19 字数 782 浏览 5 评论 0原文

如果 TreeView 按如下方式设置,什么会阻止 TreeView 虚拟化?

<TreeView 
    ItemsSource="{Binding}" 
    VirtualizingStackPanel.IsVirtualizing="True">
    <TreeView.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </TreeView.ItemsPanel>
    <TreeView.ItemContainerStyle>
        <Style
            TargetType="{x:Type TreeViewItem}">
            <Setter
                Property="IsExpanded"
                Value="{Binding IsExpanded, Mode=TwoWay}"/>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

我有一个没有虚拟化的节点,当我展开节点(并使用监听来检查)时,我创建了所有 TreeViewItems。我想知道是否存在某种容器组合会阻止 TreeView 虚拟化其内容。 (例如将其托管在 StackPanel 中)

What can stop a TreeView from virtualizing if the TreeView is set up as follows?

<TreeView 
    ItemsSource="{Binding}" 
    VirtualizingStackPanel.IsVirtualizing="True">
    <TreeView.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </TreeView.ItemsPanel>
    <TreeView.ItemContainerStyle>
        <Style
            TargetType="{x:Type TreeViewItem}">
            <Setter
                Property="IsExpanded"
                Value="{Binding IsExpanded, Mode=TwoWay}"/>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

I have one that is not virtualizing, when i expand the nodes (and use snoop to check) i have all of the TreeViewItems being created. I am wondering if there is some combination of containers that would prevent the TreeView from virtualizing its content. (like hosting it in a StackPanel for example)

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

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

发布评论

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

评论(2

以可爱出名 2024-10-11 08:54:19

问题出在造型上。经过一番研究,我们发现有一种针对 TreeView 的未命名样式(即一种带有 DataType={x:Type TreeView} ,但没有 x:Key),另一种则针对我们的 App.xaml(或等效文件)中的 TreeViewItem 它分别覆盖了每个的 ControlTemplate

这些样式没有将 ItemsPanel 设置为 VirtualizingStackPanel 的触发器,并且没有提及任何虚拟化。删除样式后,TreeView 可以正常工作。即使本地属性在 TreeView 上设置了 ItemsPanelVirtualizingStackPanel.Isvirtualizing="True",这些属性也不会传播到 TreeView 上。 code>TreeViewItems 因此 TreeView 的顶层将虚拟化,而子类别则不会(因为它们的虚拟化行为依赖于 TreeViewItem

The problem was with the styling. After some research we found that there was an unnamed style targeting the TreeView (i.e. one with DataType={x:Type TreeView} without an x:Key) and one targetting the TreeViewItem in our App.xaml (or equivalent) It was overriding the ControlTemplate for each respectively.

These styles did not have the triggers to set the ItemsPanel to a VirtualizingStackPanel and had no mention of any virtualization. When the styles are removed the TreeView works fine. Even though the local properties set the ItemsPanel and the VirtualizingStackPanel.Isvirtualizing="True" on the TreeView these properties were not being propogated to the TreeViewItems so the top level of the TreeView would virtualize whilst the sub categories would not (as their virtualization behaviour was dependant on the TreeViewItem)

就是爱搞怪 2024-10-11 08:54:19

有同样的问题。就我而言,TreeView 的初始大小不受限制(TreeView 在弹出窗口内)。因此,虚拟化面板第一次初始化了所有控件。

将TreeView的MaxHeigt设置为1000解决了这个问题。

Had the same problem. In my case, the initial size of the TreeView was not limited (TreeView is within a popup). Therfore, the virtualization panel initialized all controls for the first time.

Setting the MaxHeigt of the TreeView to 1000 solvend the problem.

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