2009-07-30 47 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虛擬化了它的所有層,而不僅僅是第一層。