How do I sort a WPF treeview that has items bound to the properties of an Item subclass?

不打扰是莪最后的温柔 提交于 2019-12-06 16:27:42
Jeremy

While the answers here provided partial answers to my question, none of them gave the answer that I needed.

The most sensible solution for this problem was to write my own object comparer for this object type and sort the underlying list and then re-bind the new list to the treeview. This allowed for comparing sublasses at any nested level which I couldn't make work any other way :)

You need to get the default view for each sub-list, and apply a CollectionViewSource sorting to it. The code you posted only affects the top level items.

Bind your TreeView.ItemsSource to the DefaultView. SortDescriptions will not change your DataList, only the View of it.

tvList.ItemsSource = bookItemsSort;

See Bea Stollnitz blog: How can I sort a hierarchy?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!