开发者

TreeView and HierarchicalDataTemplate in code

I am tryin开发者_如何转开发g to set up a TreeView with code only from an unnested list of objects with various properties. I set up SortDescriptions and GroupDescriptions, but I cannot figure out how to configure the HierarchicalDataTemplate to the CollectionViewSource groups.

Can someone give me a code-only example?

I've got:

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

...and pretty much don't know what to do at that point. I wanted the TreeView to display categories that would expand to show the animals.

Thanks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜