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