WPF TreeView HierarchicalDataTemplate - object binding with various child collections

I am trying to associate a collection with wpf TreeViewusing data patterns. Each element (Face) in the collection also contains two different collections (Cars, Books) such as a car and a book.

Here the list of objects designed to save space is simplified.

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;
}

This is how i get attached

    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;
    }

Here is the Xaml code

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

I want to see what a tree looks like.

>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 symbol is >used to display the tree folder and should not be considered in the template.

+3
source share
3 answers

, . WPF ItemsSource. CompositeCollection. (, , ) .

XAML HierarchicalDataTemplates, . local , Book, Car Person , HierarchicalDataTemplates :

 <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 :

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

, , : -)

+7

CompositeCollection - , , DataContext, Freezable. . ( . ). HierarchicalDataTemplate, ItemsSource .

, , :

public IEnumerable<object> Items
{
   get { return Books.Concat(Cars); }
}

, .

+3

You need it DataTemplate, otherwise WPF does not know how to display your object.

+1
source

Source: https://habr.com/ru/post/1733581/


All Articles