Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to set up a TreeView in code only from a "flat" list of data objects. I looked at Bea Stollnitz' example, which uses XAML, but I have not been successful in translating it to code-only. I have:

C#
ObservableCollection<animalobject> myAnimals = new ObservableCollection<animalobject>();
myAnimals.Add(new AnimalObject("mammal","cat"));
myAnimals.Add(new AnimalObject("mammal","dog"));
myAnimals.Add(new AnimalObject("bird","canary"));
myAnimals.Add(new AnimalObject("bird","eagle"));
myAnimals.Add(new AnimalObject("reptile","snake"));
myAnimals.Add(new AnimalObject("reptile","lizard"));
myAnimals.Add(new AnimalObject("reptile","dragon"));

ListCollectionView view = CollectionViewSource.GetDefaultView(myAnimals) as ListCollectionView;
view.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
view.SortDescriptions.Add(new SortDescription("Category", ListSortDirection.Ascending));
view.SortDescriptions.Add(new SortDescription("AnimalName", ListSortDirection.Descending));
</animalobject></animalobject>


I can't figure out how to set up the HierarchicalDataTemplate. I want the tree to display categories expanding to display the animals belonging to each.

Thanks!
Posted
Updated 18-Jun-11 6:10am
v2

1 solution

Something like this maybe...

StringReader stringReader = new StringReader("<ResourceDictionary xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" > <DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Key=\"animalTemplate\"> <TextBlock Text=\"{Binding Path=AnimalName}\"/> </DataTemplate> <HierarchicalDataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Key=\"categoryTemplate\" ItemsSource=\"{Binding Path=Items}\" ItemTemplate=\"{StaticResource animalTemplate}\"> <TextBlock Text=\"{Binding Path=Name}\" FontWeight=\"Bold\" /> </HierarchicalDataTemplate> </ResourceDictionary>");
XmlReader xmlReader = XmlReader.Create(stringReader);
this.Resources = (ResourceDictionary)XamlReader.Load(xmlReader);

treeView1.ItemTemplate = (DataTemplate)this.FindResource("categoryTemplate");

ObservableCollection<AnimalObject> myAnimals = new ObservableCollection<AnimalObject>();
myAnimals.Add(new AnimalObject("mammal", "cat"));
myAnimals.Add(new AnimalObject("mammal", "dog"));
myAnimals.Add(new AnimalObject("bird", "canary"));
myAnimals.Add(new AnimalObject("bird", "eagle"));
myAnimals.Add(new AnimalObject("reptile", "snake"));
myAnimals.Add(new AnimalObject("reptile", "lizard"));
myAnimals.Add(new AnimalObject("reptile", "dragon"));

ListCollectionView view = CollectionViewSource.GetDefaultView(myAnimals) as ListCollectionView;
view.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
view.SortDescriptions.Add(new SortDescription("Category", ListSortDirection.Ascending));
view.SortDescriptions.Add(new SortDescription("AnimalName", ListSortDirection.Descending));

Binding binding = new Binding("Groups");
binding.Source = view;
treeView1.SetBinding(TreeView.ItemsSourceProperty, binding);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900