C# Windows Phone是否使用HierarchycalDataTemplate进行递归?

C# Windows Phone是否使用HierarchycalDataTemplate进行递归?,c#,windows-phone-8,windows-phone,C#,Windows Phone 8,Windows Phone,我看到Windows Phone Toolkit有一个名为HierarchycalDataTemplate的元素。这对我很好,因为我想建立一个树结构 我已经看到,hierarchycaldatatemplate也包含在WPF中。这让我在这里使用了这篇文章: 它说明您应该在数据模板上设置TargetType。但是Windows Phone Toolkit中的HierarchycalDataTemplate没有该属性 此外,我想知道hierarchycaldatatemplate是做什么的,因为似乎

我看到Windows Phone Toolkit有一个名为
HierarchycalDataTemplate
的元素。这对我很好,因为我想建立一个树结构

我已经看到,
hierarchycaldatatemplate
也包含在WPF中。这让我在这里使用了这篇文章:

它说明您应该在数据模板上设置
TargetType
。但是Windows Phone Toolkit中的
HierarchycalDataTemplate
没有该属性


此外,我想知道
hierarchycaldatatemplate
是做什么的,因为似乎也没有
TreeView
控件。

有趣的问题,我刚刚检查过,实际上,在原生WP8中没有hierarchycaldatatemplate,但它在WPToolkit中。检查源代码后,它仅用于ExpanderView和MenuItem的父级的HeaderedItemControl中。
有趣的是,HierarchycalDataTemplate中的属性ItemsSourceItemTemplateItemContainerStyle甚至都不是从属属性,它们甚至没有分配到任何WPToolkit示例中。我不确定这个HierarchycalDataTemplate是否可以用于创建递归数据结构

但没关系,完全可以使用本机WP8类构建自己的递归数据结构:

让我们使用简单的递归类Node和MainViewModel以及节点集合:

public class Node
{
    public string Title { get; set; }
    public ObservableCollection<Node> Children { get; set; }
}

public class MainViewModel
{
    public ObservableCollection<Node> Nodes { get; set; }

    public MainViewModel()
    {
        Nodes = ... // load the structure here
    }
}
公共类节点
{
公共字符串标题{get;set;}
公共可观测集合子项{get;set;}
}
公共类主视图模型
{
公共ObservableCollection节点{get;set;}
公共主视图模型()
{
Nodes=…//在此处加载结构
}
}
让我们为递归显示节点创建UserControl,并为主页创建一些代码

<UserControl x:Class="WP8.HierarchicalDataTemplate.NodeUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:h="clr-namespace:WP8.HierarchicalDataTemplate">

    <Border BorderThickness="1" BorderBrush="Red">
        <StackPanel d:DataContext="{d:DesignInstance h:Node}" Margin="15">
            <TextBlock Text="{Binding Title}" />
            <ItemsControl ItemsSource="{Binding Children}"
                ItemTemplate="{StaticResource NodeNestedTemplate}" />
        </StackPanel>
    </Border>
</UserControl>

<!-- on MainPage as root object -->
<Grid>
    <ItemsControl Margin="20"
        ItemsSource="{Binding Nodes, Source={StaticResource model}}"
        ItemTemplate="{StaticResource NodeNestedTemplate}" />
</Grid>

注意,我们在NodeUserControl和名为NodeNestedTemplate的主页数据模板中都使用过。我们不能像那样定义递归DataTemplate,但我们可以创建使用此DataTemplate并放置在此DataTemplate中的UserControl-这在App.xaml的全局资源中:

...
xmlns:h="clr-namespace:WP8.HierarchicalDataTemplate">

<Application.Resources>
    <h:MainViewModel x:Key="model"/>
    <DataTemplate x:Key="NodeNestedTemplate">
        <h:NodeUserControl />
    </DataTemplate>
</Application.Resources>
...
。。。
xmlns:h=“clr命名空间:WP8.HierarchycalDataTemplate”>


希望它能帮助您实现树结构

你能分享一段代码片段吗。或者,如果有任何带有虚拟数据的示例项目亲爱的Martin,您的解决方案似乎非常有趣和有用,在此之前,我觉得实现递归对我来说是一场白费力气的追逐。请发布更多的代码,我将能够在我的应用程序中实现。谢谢:)如果你不想在Windows Phone上进行TreeView,你可以看到它