C#   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c# – 展开Wpf Treeview以支持排序大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
嗨,我创建了这个小例子,我想扩展它以支持排序.

@H_674_7@public class Country { public String Name { get; set; } public int SortOrder { get; set; } }

我的xaml:

@H_674_7@<TreeView Name="CountryTreeView" Itemssource="{Binding}"> <TreeView.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Path=NamE}"/> </DataTemplate> </TreeView.ItemTemplate> </TreeView>

代码隐藏:

@H_674_7@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方法.

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

@H_674_7@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 }

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

@H_674_7@Countries.sort(country => country.sortOrder);

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

大佬总结

以上是大佬教程为你收集整理的c# – 展开Wpf Treeview以支持排序全部内容,希望文章能够帮你解决c# – 展开Wpf Treeview以支持排序所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。