Hi:
I declared a TreeView in xaml bound to a Hierarchical object in memory, but it doesn't show the info.Here's the object structure:
public class Animals { public string Name { get; set; } public List<Kind> Kinds { get; set; } } public class Kind { public string Name { get; set; } public List<Animal> Animal { get; set; } } public class Animal { public string Name { get; set; } }
I initialize it in the Window's constructor:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var animals = new Animals {Name = "Animals Kingdom", Kinds = new List<Kind>()}; var kind = new Kind {Name = "Vertebrae", Animal = new List<Animal>()}; var animal = new Animal { Name = "Bear" }; kind.Animal.Add(animal); animal = new Animal() { Name = "Tiger" }; kind.Animal.Add(animal); animal = new Animal() { Name = "Lion" }; kind.Animal.Add(animal); animals.Kinds.Add(kind); kind = new Kind { Name = "Invertebrae", Animal = new List<Animal>() }; animal = new Animal { Name = "Fly" }; kind.Animal.Add(animal); animal = new Animal() { Name = "Grasshopper" }; kind.Animal.Add(animal); animal = new Animal() { Name = "Spider" }; kind.Animal.Add(animal); animals.Kinds.Add(kind); } }
And I declare it in xaml:
<Window.Resources><local:Animals x:Key="MyList"/><HierarchicalDataTemplate DataType = "{x:Type local:Animals}" ItemsSource = "{Binding Path=Kinds}"><TextBlock Text="{Binding Path=Name}"/></HierarchicalDataTemplate><HierarchicalDataTemplate DataType = "{x:Type local:Kind}" ItemsSource = "{Binding Path=Animal}"><TextBlock Text="{Binding Path=Name}"/></HierarchicalDataTemplate><DataTemplate DataType="{x:Type local:Animal}"><TextBlock Text="{Binding Path=Name}"/></DataTemplate></Window.Resources><DockPanel><TreeView><TreeViewItem x:Name="tree" ItemsSource="{Binding Source={StaticResource MyList}}" Header="Animals" /></TreeView></DockPanel>
The TreeView appears empty:
Any suggestions.
Rafael
Believe you can do it, and you will!!