WPF TreeView将自定义标头添加到HierarchicalDataTemplates

时间:2016-03-15 08:42:16

标签: c# wpf xaml treeview hierarchicaldatatemplate

我创建了一个简单的示例,其中包含班级列表和班级中的学生列表。这给出了一个像这样的TreeView:

school
|-class1
  |-student1
  |-student1
|-class2
...

但我想要的是这样:

school
|-CLASSES
  |-class1
    |-STUDENTS
      |-student1
      |-student1
  |-class2
  ...

我想在不改变绑定到TreeView的对象的情况下这样做。如果我可以添加自定义(CLASSES,STUDENTS等等)以某种方式命名到每个HierarchicalDataTemplate,那将是完美的。

我怎样才能做到这一点?

这是我的基础:

C#所需的课程

public class School
    {
        public string name { get; set; }
        public List<Classi> classes { get; set; }
    }
    public class Classi
    {
        public string name { get; set; }
        public List<Student> students { get; set; }
    }
    public class Student
    {
        public string name { get; set; }
    }

C#绑定到TreeView的列表

private List<object> _items = new List<object>();
        public List<object> items
        {
            get
            {
                return _items;
            }
            set
            {
                _items = value;
                NotifyOfPropertyChange(() => items);
            }
        }

C#填补我的学校

var stud1 = new Student { name = "student1" };
var stud2 = new Student { name = "student2" };
var clas1 = new Classi { name = "class1" };
clas1.students = new List<Student>();
clas1.students.Add(stud1);
var clas2 = new Classi { name = "class2" };
clas2.students = new List<Student>();
clas2.students.Add(stud2);
var school = new School();
school.name = "school";
school.classes = new List<Classi>();
school.classes.Add(clas1);
school.classes.Add(clas2);
items.Add(school);

XAML 包含HierarchicalDataTemplates的TreeView

<TreeView ItemsSource="{Binding items}">
            <TreeView.Resources>
                <HierarchicalDataTemplate ItemsSource="{Binding classes}" DataType="{x:Type src:School}">
                    <TextBlock Text="{Binding name}" />
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding students}" DataType="{x:Type src:Classi}">
                    <TextBlock Text="{Binding name}" />
                </HierarchicalDataTemplate>

                <DataTemplate DataType="{x:Type src:Student}">
                    <TextBlock Text="{Binding name}" />
                </DataTemplate >
            </TreeView.Resources>
        </TreeView>

1 个答案:

答案 0 :(得分:0)

这是一个古老的问题,但是由于我正在寻找一个非常相似的问题的答案,因此,这里提供了一种可能的解决方案(不是很通用)作为参考。

<Window.Resources>
    <HierarchicalDataTemplate x:Key="studentTemplate">
        <TextBlock Text="{Binding Path=name}" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate x:Key="classesTemplate">
        <TreeViewItem Header="{Binding Path=name}" >
            <TreeViewItem Header="STUDENTS" ItemsSource="{Binding Path=students}"
                    ItemTemplate="{StaticResource studentTemplate}"/>
        </TreeViewItem>
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate x:Key="schoolTemplate">
        <TreeViewItem Header="{Binding Path=name}" >
            <TreeViewItem Header="CLASSES" ItemsSource="{Binding Path=classes}"
                    ItemTemplate="{StaticResource classesTemplate}"/>
        </TreeViewItem>
    </HierarchicalDataTemplate>
</Window.Resources>

<TreeView Name="thisTree" ItemTemplate="{StaticResource schoolTemplate}" />

该解决方案受到thisthis的“启发”