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
模板不会在任何地方呈现。建议?
答案 0 :(得分:0)
而不是在ProjectSteps中设置ProjectsOverview中的ItemsSource
<HierarchicalDataTemplate x:Key="ProjectsOverview" ItemsSource="{Binding Steps}"