我知道这是一个老问题,但仍然是相关的我。
在我的情况下,我认为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