3

我看到 Windows Phone 工具包有一个名为 a 的元素HierarchicalDataTemplate。这对我有好处,因为我想构建一个树结构。

我已经看到它HierarchicalDataTemplate也包含在 WPF 中。这让我在这里使用了这篇文章:TreeView, HierarchicalDataTemplate and recursive Data

它指出您应该TargetType在数据模板上设置。但是HierarchicalDataTemplateWindows Phone Toolkit 中的 没有该属性。

此外,我想知道这HierarchicalDataTemplate是什么,因为似乎也无法TreeView控制。

4

1 回答 1

6

Interesting question, I've just checked and indeed, there is no HierarchicalDataTemplate in native WP8, but it is in WPToolkit. After checking the source code it's used only in HeaderedItemsControl, that is parent of ExpanderView and MenuItem.
Interesingly, the properties ItemsSource, ItemTemplate and ItemContainerStyle in HierarchicalDataTemplate are not even DependencyProperties and they are not even assigned in any WPToolkit samples. I'm not sure this HierarchicalDataTemplate is actually usable for creating recursive data structures.

But never mind, it's completely possible to build your own recursive data structure using just the native WP8 classes:

Let's have simple recursive class Node and MainViewModel with collection of nodes:

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
    }
}

Let's create our UserControl for displaying nodes recursively and some code for the MainPage.

<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>

Note we have used both in NodeUserControl and on MainPage DataTemplate named NodeNestedTemplate. We cannot define recursive DataTemplate just like that, but we can create UserControl that is using this DataTemplate and also placed inside this DataTemplate - this is in global resources in App.xaml:

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

<Application.Resources>
    <h:MainViewModel x:Key="model"/>
    <DataTemplate x:Key="NodeNestedTemplate">
        <h:NodeUserControl />
    </DataTemplate>
</Application.Resources>
...

And that's all! Here is a small screenshot of the solution when using data source with 3 levels of recursion: http://i.stack.imgur.com/zqOYe.png

Hope it helps you implementing the Tree structure!

于 2013-11-09T16:55:36.703 回答