我知道這是一個老問題,但仍然是相關的我。
在我的情況下,我認爲TreeView
不會削減它,因爲我需要正好兩層,所顯示的項目類型是不同的兩層之間。另外,我正在重構一個Expander
的列表,所以我更加單向地思考。
但後來我意識到,你可以自定義TreeView
的ItemTemplate
包括您自己的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; }
}
這裏是如何它看起來:
我Snoop檢查它來證明它是UI虛擬化。下面是裝TreeViewItem
S的數量時,應用程序小:
...這是加載TreeViewItem
S的數量時,應用程序是全屏(它不斷超越這個片段中,但你的想法):
現在,這只是事情的造型簡單的事情,使嵌套層看怎麼想!
編輯:我剛剛證實TreeView
虛擬化了它的所有層,而不僅僅是第一層。
我知道這已經有一段時間了,但是你還記得你做了什麼來解決這個問題嗎? – 2017-03-16 17:08:11
我剛剛意識到[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