如何仅从代码中的未嵌套对象列表创建树形视图? [英] how do i create a treeview from an unnested list of objects in code only?

查看:89
本文介绍了如何仅从代码中的未嵌套对象列表创建树形视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图仅从数据对象的固定"列表中以代码设置TreeView.我看了Bea Stollnitz的示例,该示例使用XAML,但是我没有成功将其转换为仅代码形式.我有:

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:

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>



我不知道如何设置HierarchicalDataTemplate.我希望树显示类别扩展以显示属于每个类别的动物.

谢谢!



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!

推荐答案

像这样的事情...

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


这篇关于如何仅从代码中的未嵌套对象列表创建树形视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆