WPF 保持 TreeView 列表排序 [英] WPF keeping a TreeView list sorted

查看:26
本文介绍了WPF 保持 TreeView 列表排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 TreeView 上有一个节点,我手动填充该节点并希望保持排序.通过用户交互,TreeViewItem 上的标题可能会发生变化,它们应该移动到列表中的适当位置.

I have a node on a TreeView that I populate manually and want to keep sorted. Through user interaction the headers on the TreeViewItem's may change and they should move to the appropriate spot in the list.

我遍历一个 foreach,创建多个 TreeViewItem 并将它们添加到父节点.需要对所有孩子进行排序.然后我添加了一个 SortDescription,如下所示.

I iterate through a foreach creating numerous TreeViewItem's and adding them to a parent node. It is all of the children that need to be sorted. I then add a SortDescription as follows.

tviParent.Items.SortDescriptions.Add(new SortDescription("Header", ListSortDirection.Ascending));

这会对初始列表进行排序,但是如果我在显示树视图项目之一后更改其标题,则该项目不会再次排序.标题文本更改,但项目在列表中的位置保持不变.

This sorts the intial list, but if I change the header for one of the tree view items after it is displayed the item does not sort again. The header text changes, but the items position in the list remains the same.

有什么我遗漏的吗?

我已经尝试清除列表并重新填充它,这会起作用,但是它会导致我的程序中出现一些问题,因为我有很多关于何时更改所选项目的逻辑以及列表中的树视图项目之一我正在清除的被选中它会在我清除列表时调用所有这些逻辑,然后在重建列表后以编程方式重新选择项目时再次调用.

I have tried clearning the list and repopulating it, which will work, however it causes some isues in my program as I have a lot of logic for when the selected item is changed and since one of the tree view items in the list that I am clearing is selected it invokes all of this logic when I clear the list and then again when I programmatically reselect the item after rebuilding the list.

推荐答案

items 集合必须在其属性发生任何更改时刷新

The items collection will have to be refreshed upon any change to its property

在标题编辑后尝试以下代码....

try the following code after the header edit ....

  tviParent.Items.Refresh();

如果上面的代码不起作用,那么在每次编辑后尝试下面的代码......

if the code above does not work then try code below after each edit ...

  tviParent.Items.SortDescriptions.Clear()
  tviParent.Items.SortDescriptions.Add(new SortDescription("Header", ListSortDirection.Ascending));  

这篇关于WPF 保持 TreeView 列表排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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