2009-07-30 48 views
2

我已经将自己编码为一个在这一个的泡菜。我正在编写一个自定义的WPF控件,它类似于This MSDN article中描述的TreeListView以及网络上的许多其他地方。这个东西的一大堆东西是定制的,除了在虚拟化方面,它还能很好地达到我的目标。我的覆盖TreeViewTreeViewItem模板都使用VirtualizingStackPanel s来呈现它们的项目,并且我已验证所有这些都是按预期创建的。虚拟化在根级别项目上正常工作(只有ScrollViewer中当前可见的用户界面元素被制作出来),并且TreeView东西负责不为折叠节点生成元素。扩展节点时会出现问题 - 节点中的每个子节点都会烧制所有元素,即使是离屏的数千个元素也是如此。将嵌套容器(virtualizingstackpanel)虚拟化为WPF中的单个父滚动条

在我看来,我所需要做的只是将内嵌的VirtualizingStackPanel s的scrollowner属性设置为与默认根级别VSP相关的主滚动视图,但我阅读了MSFT海报here说这不起作用。

不幸的是,这个东西很慢,因为泥浆没有发生虚拟化,所以我需要想出一些解决方案。任何建议将不胜感激。

+0

我知道这已经有一段时间了,但是你还记得你做了什么来解决这个问题吗? – 2017-03-16 17:08:11

+0

我刚刚意识到[David的帐户已暂时不活动](http://stackoverflow.com/users/17784/david-hay?post-filters=All&post-sorts=Newest)。所以对于任何人后来,[见我的答案](http://stackoverflow.com/a/42842476/3063273) – 2017-03-16 18:48:31

回答

0

我有一个链接,指向不使用虚拟化的堆栈面板的列表,但由于某种原因页面出现空白。这里是一个谈论了一点另一页:

http://www.designerwpf.com/2008/02/12/listview-and-listbox-performance-issues/

,甚至链接到一个IM议论纷纷,但其始终空白。如果您在该页面上查看与Mark Shurmer博客的链接,下面是如果你想尝试一下链接:

http://itknowledgeexchange.techtarget.com/wpf/listview-is-it-really-too-slow/

比亚Stollnitz也有一些关于它的文章,可能会帮助:

Part 1 Part 2 Part 3

如果您张贴一些的代码,有人可能能够解开意大利面,以帮助您更好地实施。

编辑:找到一个链接,可能工作(谢谢archive.org !!!): http://web.archive.org/web/20080104163725/http://itknowledgeexchange.techtarget.com/wpf/listview-is-it-really-too-slow/

0

我知道这是一个老问题,但仍然是相关的我。

在我的情况下,我认为TreeView不会削减它,因为我需要正好两层,所显示的项目类型是不同的两层之间。另外,我正在重构一个Expander的列表,所以我更加单向地思考。

但后来我意识到,你可以自定义TreeViewItemTemplate包括您自己的HierarchicalDataTemplate,并且您可以自定义HierarchicalDataTemplate用自己的ItemTemplate定制DataTemplate ...普雷斯托!每一层都有两层不同的东西!

所以我的意思是TreeView是足够灵活,你应该尽量不要创建自己的自定义控件。

这里就是我所做的:

XAML:

<Page x:Class="Foo.Views.TreePage" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:Foo.Views" 
     xmlns:viewModel="clr-namespace:Foo.ViewModels" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300" 
     Title="Tree page" 
     d:DataContext="{d:DesignInstance Type=viewModel:TreeModel}"> 
    <TreeView 
     VirtualizingPanel.IsVirtualizing="True" 
     VirtualizingPanel.VirtualizationMode="Recycling" 
     ScrollViewer.CanContentScroll="True" 
     VirtualizingPanel.ScrollUnit="Pixel" 
     ItemsSource="{Binding Values}"><!-- IList<Person> --> 
     <TreeView.ItemTemplate><!-- Template for first layer, which has a HierarchicalDataTemplate so that this layer will expand --> 
      <HierarchicalDataTemplate 
       ItemsSource="{Binding Expenses}"><!-- IList<Expense> --> 
       <HierarchicalDataTemplate.ItemTemplate><!-- Template for the second layer, which has a DataTemplate instead of HierarchicalDataTemplate so that this layer won't expand --> 
        <DataTemplate> 
         <TextBlock Text="{Binding Amount}"/><!-- Expense amount in dollars --> 
        </DataTemplate> 
       </HierarchicalDataTemplate.ItemTemplate> 

       <TextBlock Text="{Binding Name}"/><!-- Person name --> 
      </HierarchicalDataTemplate> 
     </TreeView.ItemTemplate> 
    </TreeView> 
</Page> 

Person类:

public class Person 
{ 
    public string Name { get; set; } 
    public List<Expense> Expenses { get; set; } 
} 

Expense类:

public class Expense 
{ 
    public double Amount { get; set; } 
} 

这里是如何它看起来:

Screenshot of the above code in action

Snoop检查它来证明它是UI虚拟化。下面是装TreeViewItem S的数量时,应用程序小:

Screenshot showing the number of elements when the app is small

...这是加载TreeViewItem S的数量时,应用程序是全屏(它不断超越这个片段中,但你的想法):

Screenshot showing that there are more elements when the app is fullscreen

现在,这只是事情的造型简单的事情,使嵌套层看怎么想!

编辑:我刚刚证实TreeView虚拟化了它的所有层,而不仅仅是第一层。