c# - 扩展 Wpf Treeview 以支持排序

标签 c# wpf c#-4.0 treeview

您好,我创建了这个小示例,我想扩展它以支持排序。

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 值对 Countries 进行排序。

它需要能够即时执行此操作。 因此,例如,如果我将 SortOrder = 10 更改为 Name = "Denmark",TreeView 将自动反射(reflect)这一点。

最佳答案

我认为 TreeView 没有默认排序。您可以在将项目输入集合之前对其进行排序,或者覆盖 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);

我喜欢覆盖它,因为它让我可以向它添加额外的功能,例如 IndexOfAddRange/RemoveRange

关于c# - 扩展 Wpf Treeview 以支持排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5487927/

相关文章:

c# - 绑定(bind)重定向 hell

c# - XAML 中的动画按钮背景颜色

c# - 使用 WebClient 将数据发布到 SSRS 报告服务器

c# - 对列表中的值进行分组并使用 C# 将输出转换为 JSON

c#-4.0 - 为什么 Byte 不是枚举的默认支持类型?

c# - 对实现接口(interface)的对象进行 LINQ 查询

c# - 闹钟应用帮助(AI)

c# - 每个进程是否只有一个静态变量实例?

c# - 加载具有较低权限的程序集

WPF:消失的图标