TreeView绑定到具有另一个类列表的类(依此类推)

时间:2014-09-07 15:01:47

标签: c# wpf binding treeview

我尝试了所有内容,但无论我如何配置HierarchicalDataTemplate,它始终只显示集合的顶级(StarSystem项和StarSystemName属性)

这就是我现在配置TreeView的方式:

<TreeView x:Name="StarSystemTreeView" ItemsSource="{Binding StarSystems, ElementName=window}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding}">
            <TextBlock Text="{Binding Path=StarSystemName}"/>
            <HierarchicalDataTemplate.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Path=Planets}">
                    <TextBlock Text="{Binding Path=PlanetNumber}"/>
                    <HierarchicalDataTemplate.ItemTemplate>
                        <HierarchicalDataTemplate ItemsSource="{Binding Path=Moons}">
                            <TextBlock Text="{Binding Path=MoonNumber}"/>
                            <HierarchicalDataTemplate.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Path=MoonMinerals}"/>
                                </DataTemplate>
                            </HierarchicalDataTemplate.ItemTemplate>
                        </HierarchicalDataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

window是我的MainWindow课程,其中StarSystems已初始化:

public partial class MainWindow
{
    private ObservableCollection<StarSystem> _starSystems = new ObservableCollection<StarSystem>
    {
        new StarSystem("FooSystem",
            new ObservableCollection<Planet>
            {
                new Planet(1, new ObservableCollection<Moon>
                {
                    new Moon(1, new ObservableCollection<string>
                    {
                        "FooMineral"
                    })
                })
            })
    };

    public ObservableCollection<StarSystem> StarSystems
    {
        get { return _starSystems; }
        set { _starSystems = value; }
    }
}

我要绑定到TreeView的3个类是在MainWindow类之外的同一命名空间中定义的:

[NotifyPropertyChanged]
public class StarSystem
{
    public StarSystem()
        : this("", new ObservableCollection<Planet>())
    {
    }
    public StarSystem(string starSystemName, ObservableCollection<Planet> planets)
    {
        StarSystemName = starSystemName;
        Planets = planets;
    }

    public string StarSystemName { get; set; }
    public ObservableCollection<Planet> Planets { get; set; }
}

[NotifyPropertyChanged]
public class Planet
{
    public Planet()
        : this(0, new ObservableCollection<Moon>())
    {
    }
    public Planet(int planetNumber, ObservableCollection<Moon> moons)
    {
        PlanetNumber = planetNumber;
        Moons = moons;
    }

    public int PlanetNumber { get; set; }
    public ObservableCollection<Moon> Moons { get; set; }
}

[NotifyPropertyChanged]
public class Moon
{
    public Moon()
        : this(0, new ObservableCollection<string>())
    {
    }
    public Moon(int moonNumber, ObservableCollection<string> moonMinerals)
    {
        MoonNumber = moonNumber;
        MoonMinerals = moonMinerals;
    }

    public int MoonNumber { get; set; }
    public ObservableCollection<string> MoonMinerals { get; set; }
}

这是启动应用程序后的样子:

我过去3天一直试图解决这个问题,我想我已尽可能地尝试配置HierarchicalDataTemplate所以我可能只是做错了什么...

1 个答案:

答案 0 :(得分:1)

ItemTemplate有点搞砸了。您已将ItemSource设置为一个层次结构级别。它应该是:

<TreeView x:Name="StarSystemTreeView"
          ItemsSource="{Binding StarSystems, ElementName=window}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Path=Planets}">
            <TextBlock Text="{Binding Path=StarSystemName}"/>
            <HierarchicalDataTemplate.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Path=Moons}">
                    <TextBlock Text="{Binding Path=PlanetNumber}"/>
                    <HierarchicalDataTemplate.ItemTemplate>
                        <HierarchicalDataTemplate
                                    ItemsSource="{Binding Path=MoonMinerals}">
                            <TextBlock Text="{Binding Path=MoonNumber}"/>
                            <HierarchicalDataTemplate.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding}"/>
                                </DataTemplate>
                            </HierarchicalDataTemplate.ItemTemplate>
                        </HierarchicalDataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>