WPF:TreeView 虚拟化不起作用
如果 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题出在造型上。经过一番研究,我们发现有一种针对 TreeView 的未命名样式(即一种带有
DataType={x:Type TreeView}
,但没有x:Key
),另一种则针对我们的 App.xaml(或等效文件)中的TreeViewItem
它分别覆盖了每个的ControlTemplate
。这些样式没有将
ItemsPanel
设置为VirtualizingStackPanel
的触发器,并且没有提及任何虚拟化。删除样式后,TreeView 可以正常工作。即使本地属性在TreeView
上设置了ItemsPanel
和VirtualizingStackPanel.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 anx:Key
) and one targetting theTreeViewItem
in our App.xaml (or equivalent) It was overriding theControlTemplate
for each respectively.These styles did not have the triggers to set the
ItemsPanel
to aVirtualizingStackPanel
and had no mention of any virtualization. When the styles are removed the TreeView works fine. Even though the local properties set theItemsPanel
and theVirtualizingStackPanel.Isvirtualizing="True"
on theTreeView
these properties were not being propogated to theTreeViewItems
so the top level of the TreeView would virtualize whilst the sub categories would not (as their virtualization behaviour was dependant on theTreeViewItem
)有同样的问题。就我而言,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.