Wpf Xaml - hierarchical TreeView data templates - multiple element sources

I have a class like this (describes a class in C # and its fields, methods, etc.):

public class CSharpType { public string Name { get; private set; } public List<CSharpMethod> Methods { get; private set; } public List<CSharpField> Fields { get; private set; } public List<CSharpProperty> Properties { get; private set; } .... } 

CShartpType collection in the returned:

  public List<CSharpType> TypeCollection { get { TypeCollection kolekcjaTypow = metricsCollection.Types; Dictionary<string, CSharpType> typy = kolekcjaTypow.TypeDictionary; var result = typy.Values.ToList(); return result; } } 

Each field, method, property has a "Name" property and I want to have a TreeView (for example):

 Person + Fields + field1 name from Fields collection + field2 name from Fields collection ... + Methods .... + Properties 

What does xaml image look like? Thank you for your help.

+4
source share
1 answer

If the classes are as follows:

 public class FatherClass { public string Name { get; set; } public List<ChildClass> Children { get; set; } } public class ChildClass { public string Name { get; set; } } 

and in ctor window i have the following data:

  List<FatherClass> list = new List<FatherClass>(); list.Add(new FatherClass { Name = "First Father" }); list.Add(new FatherClass { Name = "Second Father" }); list[0].Children = new List<ChildClass>(); list[1].Children = new List<ChildClass>(); list[0].Children.Add(new ChildClass { Name = "FirstChild" }); list[0].Children.Add(new ChildClass { Name = "SecondChild" }); list[1].Children.Add(new ChildClass { Name = "ThirdChild" }); list[1].Children.Add(new ChildClass { Name = "ForthChild" }); this.DataContext = list; 

then to create a hierarchical data binding you must define two hierarchical data sets in the resources to "capture" the corresponding data types, for example:

  <Grid.Resources> <HierarchicalDataTemplate DataType="{x:Type my:FatherClass}" ItemsSource="{Binding Children}" > <TreeViewItem Header="{Binding Name}" /> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type my:ChildClass}" > <TreeViewItem Header="{Binding Name}" /> </HierarchicalDataTemplate> </Grid.Resources> 

and then the tree image syntax should be:

  <TreeView ItemsSource="{Binding }"> </TreeView> 
0
source

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


All Articles