I have a class like this (describes class in C# and its fields, methods and so on):
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; }
....
}
Collection of CShartpType in returned by:
public List<CSharpType> TypeCollection
{
get
{
TypeCollection kolekcjaTypow = metricsCollection.Types;
Dictionary<string, CSharpType> typy = kolekcjaTypow.TypeDictionary;
var result = typy.Values.ToList();
return result;
}
}
Every Field, Method, Property has a "Name" property and I want to have TreeView (e.g):
Person
+ Fields
+ field1 name from Fields collection
+ field2 name from Fields collection
...
+ Methods
....
+ Properties
How sholud the xaml look like ? Thank for your help
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 the ctor of the 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 in order to create hierarchical databinding, you should define two hierarchical datatemplates in the resources to 'catch' the relevant data types, like so:
<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 syntax of the treeview should be:
<TreeView ItemsSource="{Binding }">
</TreeView>
精彩评论