WPF TreeView HierarchicalDataTemplate-绑定到具有不同子集合的对象 [英] WPF TreeView HierarchicalDataTemplate - binding to object with different child collections

查看:442
本文介绍了WPF TreeView HierarchicalDataTemplate-绑定到具有不同子集合的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用数据模板将集合绑定到wpf TreeView控件.集合中的每个项目(人)还包含两个不同的类型为car和book的集合(Cars,Books).

I am trying to bind a collection to wpf TreeView control using data templates. Each item(Person) in the collection also contains two different collections(Cars, Books) of type car and book.

这里是涉及对象的简化列表,以节省空间.

Here's simplified list of the objects involved to save space.

public class Person
{
  public string Name
  public List<Book> Books;
  public List<Car> Cars;
}

public class Book
{
  public string Title
  public string Author
}

public class Car
{
  public string Manufacturer;
  public string Model;
}

这就是我的束缚方式

    public MainWindow()
    {
        InitializeComponent();

        this.treeView1.ItemsSource = this.PersonList();
    }

    public List<Person> PersonList()
    {
        List<Person> list = new List<Person>();


        Book eco = new Book { Title = "Economics 101", Author = "Adam Smith"};
        Book design = new Book { Title = "Web Design", Author = "Robins" };

        Car corola = new Car { Manufacturer = "Toyota", Model = "2005 Corola"};
        Car ford = new Car { Manufacturer = "Ford", Model = "2008 Focus"};

        Person john = new Person { Name = "John", Books = new ObservableCollection<Book> { eco, design }, Cars = new ObservableCollection<Car> { corola } };

        Person smith = new Person { Name = "Smith", Books = new ObservableCollection<Book> { eco, design }, Cars = new ObservableCollection<Car> { ford } };

        list.AddRange(new[] {john, smith });
        return list;
    }

这是Xaml代码

<Grid>
    <TreeView  Name="treeView1">
    </TreeView>
</Grid>

我希望看到树状显示的样子.

I am looking to see the tree display to look like this.

>John
  >Books
    Economics 101 : Adam Smith
    Web Design    : Robins
  >Cars
    Totota : 2005 Corola
>Smith
  >Books
    Economics 101 : Adam Smith
    Web Design    : Robins
  >Cars
    Ford: 2008 Focus

此符号>用于显示树文件夹,不应在模板中考虑.

this sign > is used to show the tree folder and should not be considered in the template.

推荐答案

这有点复杂,因为您的树有两个不同的子集合. WPF不支持具有多个ItemsSource定义的方案.因此,您需要将这些集合合并到 CompositeCollection .复合元素(即汽车,图书)的类型匹配将自动完成.

It is a bit complicated since your tree has two different child collections. WPF does not support a scenario with multiple ItemsSource definitions. Therefore you need to combine those collection into a CompositeCollection. The type matching of the composite elements (i.e. Car, Book) will be done automatically.

在XAML中,您需要定义所谓的 HierarchicalDataTemplates 与您的类型定义匹配.如果local指向其中定义了BookCarPerson的名称空间,则简化的HierarchicalDataTemplates可能看起来像这样:

In XAML you need to define so-called HierarchicalDataTemplates that match your type definitions. If local points to the namepace where Book, Car and Person are defined, the simplified HierarchicalDataTemplates could look like this:

 <HierarchicalDataTemplate DataType="{x:Type local:Person}" 
                              ItemsSource="{Binding CompositeChildren}">
        <TextBlock Text="{Binding Path=Name}" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="{x:Type local:Book}">
        <TextBlock Text="{Binding Path=Title}" />
        <!-- ... -->
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="{x:Type local:Car}">
        <TextBlock Text="{Binding Path=Model}" />
        <!-- ... -->
    </HierarchicalDataTemplate>

然后,您需要将集合连接到树控件.有几种方法可以做到这一点,最简单的方法是在Window类中定义一个属性并定义一个Binding:

Then you need to hook up your collection to the tree control. There are a few possibilities to do this, the easiest would be to define a property in your Window class and define a Binding:

<TreeView Items={Binding ElementName=myWindow, Path=Persons}/>

这应该为您指明正确的方向,但是不要把我的代码准备好编译:-)

This should point you into the right direction, but don't take my code as compile ready :-)

这篇关于WPF TreeView HierarchicalDataTemplate-绑定到具有不同子集合的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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