递归DataTemplates可能吗? [英] Are recursive DataTemplates possible?

查看:73
本文介绍了递归DataTemplates可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的课程:

public class Section
{
    private IEnumerable<Section> sections;
    private IEnumerable<KeyValuePair<string, string>> attributes

    public string Name {get;set;}
    public IEnumerable<Section> Sections
    {
        get {return sections;}
    }
    public IEnumerable<KeyValuePair<string, string>> Attributes
    {
        get {return attributes;}
    }

    public Section(...)
    {
        //constructor logic
    }
}

哪个包含在另一个类中,可以称之为 OtherClass 出于争论的考虑,将其环绕并在ObjectDataProvider中的WPF中使用。

Which is contained in another class, lets call it OtherClass for sake of argument, that wraps around it and is used in WPF in an ObjectDataProvider.

如您所见, Section 的实例可以包含 Section 的许多其他实例。

As you can see, an instance of Section can contain many other instances of Section.

XAML中是否有一种方法可以创建处理此递归的模板?

Is there a way in XAML to create a template that deals with this recursion?

到目前为止,这是我得到的:

This is what I've got so far:

<UserControl x:Class="OtherClassEditor"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:OtherClassNS="clr-namespace:NameSpace"
    Height="300" Width="300">
    <UserControl.Resources>
        <ObjectDataProvider ObjectType="{x:Type OtherClassNS:OtherClass}" x:Key="ViewModel" />
        <DataTemplate x:Key="listViewAttr">
            <WrapPanel>
                <TextBlock Text="{Binding Path=Key}"></TextBlock><TextBlock Margin="0,0,4,0">: </TextBlock>
                <TextBlock Text="{Binding Path=Value}"></TextBlock>
            </WrapPanel>
        </DataTemplate>
        <DataTemplate x:Key="listViewSection">
            <StackPanel>
                <WrapPanel Margin="0,0,0,8">
                    <TextBlock Margin="0,0,4,0">Section:</TextBlock>
                    <TextBlock Text="{Binding Path=Name}"/>
                </WrapPanel>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="listViewData">
            <StackPanel>
                <WrapPanel Margin="0,0,0,8">
                    <TextBlock Margin="0,0,4,0">Section: </TextBlock>
                    <TextBlock Text="{Binding Path=Name}"/>
                    <ListView DataContext="{Binding Path=Sections}" ItemTemplate="{StaticResource listViewSection}" ItemsSource="{Binding Sections}">
                    </ListView>
                    <ListView DataContext="{Binding Path=Attributes}" ItemsSource="{Binding Attributes}" ItemTemplate="{StaticResource listViewAttr}">

                    </ListView>
                </WrapPanel>
            </StackPanel>    
        </DataTemplate>
    </UserControl.Resources>
    <Grid>
        <ListView DataContext="{StaticResource ViewModel}" ItemTemplate="{StaticResource listViewData}" ItemsSource="{Binding Sections}">
        </ListView>
    </Grid>
</UserControl>

但是我无法递归,因为 DataTemplate 只能引用在它之前声明的一个。

But I can't get recursion, as a DataTemplate can only reference one that is declared before it.

这可以在XAML中完成吗?如果是这样,怎么办?这是我在后面的代码中必须要做的吗?

Can this be done in XAML? If so, how? Is this something that I'll have to do in code behind?

DataTemplates甚至还可以吗?有没有更好的方法来显示和编辑此数据?

Are DataTemplates even the way to go? Is there a better way to display and edit this data?

推荐答案

也许我误解了您的情况,但是 HierarchicalDataTemplate 您在寻找什么?

Perhaps I'm misunderstanding your scenario, but is HierarchicalDataTemplate what you're looking for?

这篇关于递归DataTemplates可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆