加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 展开Wpf Treeview以支持排序

发布时间:2020-12-16 01:42:37 所属栏目:百科 来源:网络整理
导读:嗨,我创建了这个小例子,我想扩展它以支持排序. public class Country{ public string Name { get; set; } public int SortOrder { get; set; }} 我的xaml: TreeView Name="CountryTreeView" ItemsSource="{Binding}" TreeView.ItemTemplate DataTemplate Te
嗨,我创建了这个小例子,我想扩展它以支持排序.

public class Country
{
    public string Name { get; set; }
    public int SortOrder { get; set; }
}

我的xaml:

<TreeView Name="CountryTreeView" ItemsSource="{Binding}">
  <TreeView.ItemTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding Path=Name}"/>
    </DataTemplate>
  </TreeView.ItemTemplate>
</TreeView>

代码隐藏:

readonly ObservableCollection<Country> Countries;

public MainWindow()
{
   InitializeComponent();

   Countries = new ObservableCollection<Country>
   {
       new Country{Name = "Denmark",SortOrder = 0},new Country{Name = "Norway",SortOrder = 1},new Country{Name = "Sweden",SortOrder = 2},new Country{Name = "Iceland",SortOrder = 3},new Country{Name = "Greenland",SortOrder = 4},};

   CountryTreeView.DataContext = Countries;
}

我想让Treeview根据SortOrder值对Country进行排序.

它需要能够即时执行此操作.
因此,如果我为Name =“Denmark”更改SortOrder = 10,TreeView将自动反映这一点.

解决方法

我认为TreeViews没有默认排序.您可以在将项目输入集合之前对项目进行排序,也可以覆盖ObservableCollection以包含Sort方法.

我在我的一个项目中覆盖了它:

public class SortableObservableCollection<T> : ObservableCollection<T>
{
    // Constructors
    public SortableObservableCollection() : base(){}
    public SortableObservableCollection(List<T> l) : base(l){}
    public SortableObservableCollection(IEnumerable<T> l) :base (l) {}

    #region Sorting

    /// <summary>
    /// Sorts the items of the collection in ascending order according to a key.
    /// </summary>
    /// <typeparam name="TKey">The type of the key returned by <paramref name="keySelector"/>.</typeparam>
    /// <param name="keySelector">A function to extract a key from an item.</param>
    public void Sort<TKey>(Func<T,TKey> keySelector)
    {
        InternalSort(Items.OrderBy(keySelector));
    }

    /// <summary>
    /// Sorts the items of the collection in descending order according to a key.
    /// </summary>
    /// <typeparam name="TKey">The type of the key returned by <paramref name="keySelector"/>.</typeparam>
    /// <param name="keySelector">A function to extract a key from an item.</param>
    public void SortDescending<TKey>(Func<T,TKey> keySelector)
    {
        InternalSort(Items.OrderByDescending(keySelector));
    }

    /// <summary>
    /// Sorts the items of the collection in ascending order according to a key.
    /// </summary>
    /// <typeparam name="TKey">The type of the key returned by <paramref name="keySelector"/>.</typeparam>
    /// <param name="keySelector">A function to extract a key from an item.</param>
    /// <param name="comparer">An <see cref="IComparer{T}"/> to compare keys.</param>
    public void Sort<TKey>(Func<T,TKey> keySelector,IComparer<TKey> comparer)
    {
        InternalSort(Items.OrderBy(keySelector,comparer));
    }

    /// <summary>
    /// Moves the items of the collection so that their orders are the same as those of the items provided.
    /// </summary>
    /// <param name="sortedItems">An <see cref="IEnumerable{T}"/> to provide item orders.</param>
    private void InternalSort(IEnumerable<T> sortedItems)
    {
        var sortedItemsList = sortedItems.ToList();

        foreach (var item in sortedItemsList)
        {
            Move(IndexOf(item),sortedItemsList.IndexOf(item));
        }
    }

    #endregion // Sorting
}

然后你会通过调用类似的东西对它进行排序

Countries.Sort(country => country.SortOrder);

我喜欢覆盖它,因为它允许我添加其他功能,如IndexOf或AddRange / RemoveRange

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读