问题描述
我正在尝试弄清楚这个虚拟化功能,我不确定我是否理解错误或发生了什么,但我正在使用 ANTS 内存分析器来检查虚拟化 TreeView 中的项目数量,而且还在不断增加.我有一个包含 1,001 个项目(1 个根,1000 个子项目)的 TreeView,而且我总是最多获得 1,001 个 TreeViewItems、1,001 个 ToggleButtons 和 1,001 个 TextBlocks.虚拟化不是应该重用这些项目吗?如果是这样,为什么我每个都有 1,001 个?此外,CleanUpVirtualizedItem 永远不会触发.
I'm trying to figure out this virtualization feature, I'm not sure if I'm understanding it wrong or what's going on, but I'm using the ANTS memory profiler to check the number of items in a virtualized TreeView, and it just keeps increasing. I have a TreeView with 1,001 items (1 root, 1000 sub-items), and I always get up to 1,001 TreeViewItems, 1,001 ToggleButtons and 1,001 TextBlocks. Isn't virtualization supposed to re-use the items? If so, why would I have 1,001 of each? Also, the CleanUpVirtualizedItem never fires.
让我知道我是否理解错误,以及您是否有关于如何使用它的资源.我在互联网上搜索过,但没有找到任何有用的东西.
Let me know if I'm understanding this wrong and if you have resources on how to use this. I've searched over the internet but haven't found anything useful.
即使是树使用的内存也是从aporx增长的.当我展开并滚动浏览所有项目时,大小为 4mb 到 12mb.
Even the memory used by the tree grows from aporx. 4mb to 12mb when I expand and scroll through all the items.
让我知道谢谢.
这是我的代码.
XAML:
<Window x:Class="RadTreeViewExpandedProblem.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TreeView x:Name="treeView"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.CleanUpVirtualizedItem="TreeView_CleanUpVirtualizedItem">
<TreeView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</TreeView.ItemsPanel>
</TreeView>
</Grid>
</Window>
C#:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
TreeViewItem rootItem = new TreeViewItem() { Header = "Item Level 0" };
for (int i = 0; i < 1000; i++)
{
TreeViewItem itemLevel1 = new TreeViewItem() { Header = "Item Level 1" };
itemLevel1.Items.Add(new TreeViewItem());
rootItem.Items.Add(itemLevel1);
}
treeView.Items.Add(rootItem);
}
private void TreeView_CleanUpVirtualizedItem(object sender, CleanUpVirtualizedItemEventArgs e)
{
}
}
推荐答案
好吧,问题是虚拟化只在 TreeView 使用 Binding 时有效,而不是像我的例子那样在代码中一一生成节点时.真可惜.
Ok, the problem is that virtualization only works when the TreeView is using Binding, and not when the nodes are generated one by one in the code like my example. What a bummer.
这篇关于WPF TreeView 虚拟化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!