TreeView没有显示二级孩子

时间:2015-08-27 21:42:58

标签: c# wpf xaml treeview

XAML是这样的:

<UserControl.Resources>
    <!-- templates -->
    <HierarchicalDataTemplate x:Key="ProjectSteps" ItemsSource="{Binding Steps}">
        <TextBlock Text="{Binding Name}" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate x:Key="ProjectsOverview" ItemTemplate="{StaticResource ProjectSteps}">
        <TextBlock Text="{Binding Name}" />
    </HierarchicalDataTemplate>
</UserControl.Resources>

<TreeView HorizontalAlignment="Stretch" ItemsSource="{Binding Projects, UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{StaticResource ProjectsOverview}">
</TreeView>

Projects是我的ViewModel上的一个属性,它来自这个类:

public class ProjectsRepository : IProjectsRepository {
    public IEnumerable<Project> GetProjects() {
        return new List<Project> {
            new Project {
                Name = "Proj1",
                Steps = new List<Step> {
                    new Step { Name = "Step1" },
                    new Step { Name = "Step2" },
                    new Step { Name = "Step3" }
                }
            },
            new Project {
                Name = "Proj2",
                Steps = new List<Step> {
                    new Step { Name = "OtherStep1" },
                    new Step { Name = "OtherStep2" },
                    new Step { Name = "OtherStep3" },
                    new Step { Name = "OtherStep4" }
                }
            } };
    }
}

显示第一级项目(Proj1,Proj2),但不显示第二级项目(因此每个Step或OtherStep)。我甚至看不到项目名称旁边的下拉指示器,因此似乎第二个ProjectSteps模板不会在任何地方呈现。建议?

1 个答案:

答案 0 :(得分:0)

而不是在ProjectSteps中设置ProjectsOverview中的ItemsSource

<HierarchicalDataTemplate x:Key="ProjectsOverview" ItemsSource="{Binding Steps}"