展开WPF树视图支持排序

编程入门 行业动态 更新时间:2024-10-07 00:25:36
本文介绍了展开WPF树视图支持排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好我创造了这个小例子,我想将它扩大到支持排序。

Hi I created this small example, and I would like to expand it to support Sorting.

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

我的XAML

My 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; }

我想有可能使树视图,以根据国家排序在SortOrder的值。

I would like to make it possible for the Treeview to Sort the Countries depending on the SortOrder value.

它需要能够做到这一点的飞行。 所以,如果我恩。改SortOrder的= 10 NAME =丹麦树视图会自动反映这一点。

It needs to be able to do this on the fly. So if i ex. changed SortOrder = 10 for Name = "Denmark" the treeview would automatically reflect this.

推荐答案

我不认为这是一个默认排序为的TreeView。您可以将项目之前,将它们输入收集整理,或覆盖的ObservableCollection,包括排序方法。

I don't think there is a default sort for TreeViews. You can either sort the items prior to entering them into the collection, or overwrite the ObservableCollection to include a Sort method.

我将其覆盖在我的项目之一:

I overwrite it in one of my projects:

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 }

然后,您可以通过调用类似

You would then sort it by calling something like

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

我喜欢覆盖它,因为它让我给它添加更多的功能,以及如的IndexOf 或的AddRange / RemoveRange

更多推荐

展开WPF树视图支持排序

本文发布于:2023-11-12 20:51:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582505.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!