如何使用Xaml中的SortDescriptions对TreeView项进行排序?

时间:2011-04-19 21:16:26

标签: c# .net wpf xaml sorting

我有一个Layers绑定到TreeView的列表,其中每个实例都有一个Effects列表。我通过HierarchicalDataTemplate显示它们,它工作得很好,但我尝试使用SortDescriptions对它们进行排序。

我不知道如何在xaml中执行此操作,但这样做只对第一级别的项目进行排序,而不是子项目:

ICollectionView view = CollectionViewSource.GetDefaultView ( treeView1.ItemsSource );
view.SortDescriptions.Add ( new SortDescription ( "Name", ListSortDirection.Ascending ) );

我首先尝试按.Color排序,然后按.Name排序。

有什么想法吗?

编辑:我添加了这段代码:

<Window.Resources>

    <CollectionViewSource x:Key="SortedLayers" Source="{Binding AllLayers}">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="Color" />
            <scm:SortDescription PropertyName="Name" />
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>

</Window.Resources>

但这仍然只适用于第一级层次结构。如何为每个图层指定它。影响集合?

3 个答案:

答案 0 :(得分:18)

我建议使用转换器对子项进行排序。 像这样:

<TreeView Name="treeCategories" Margin="5" ItemsSource="{Binding Source={StaticResource SortedLayers}}">
<TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Effects, Converter={StaticResource myConverter}, ConverterParameter=EffectName}">
        <TextBlock Text="{Binding Path=LayerName}" />
        <HierarchicalDataTemplate.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=EffectName}" />
            </DataTemplate>
        </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate>
</TreeView.ItemTemplate>

和转换器:


public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        System.Collections.IList collection = value as System.Collections.IList;
        ListCollectionView view = new ListCollectionView(collection);
        SortDescription sort = new SortDescription(parameter.ToString(), ListSortDirection.Ascending);
        view.SortDescriptions.Add(sort);

        return view;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

答案 1 :(得分:1)

我发现使用多转换器更好:

using System;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Data;

namespace Converters
{
    [ValueConversion(typeof(object[]), typeof(ListCollectionView))]
    public class IListToListCollectionViewConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var Length = values.Length;
            if (Length >= 1 && Length < 3)
            {
                var IList = values[0] as IList;

                var SortName = string.Empty;
                if (Length > 1)
                    SortName = values[1].ToString();

                var SortDirection = ListSortDirection.Ascending;
                if (Length > 2)
                    SortDirection = values[2] is ListSortDirection ? (ListSortDirection)values[2] : (values[2] is string ? (ListSortDirection)Enum.Parse(typeof(ListSortDirection), values[2].ToString()) : SortDirection);

                var Result = new ListCollectionView(IList);
                Result.SortDescriptions.Add(new SortDescription(SortName, SortDirection));
                return Result;
            }
            return null;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

答案 2 :(得分:0)

这不是基于XAML的解决方案,但我遇到了同样的问题,发现了如下所示的解决方案,

我假设您有3个类似的类:AllLayers,Lays&Effects

class AllLayers
{
    public AllLayers()
    {
        layers = new ObservableCollection<Layers>();
        var collectionView = CollectionViewSource.GetDefaultView(layers);
        collectionView.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
        collectionView.SortDescriptions.Add(new SortDescription("Color", ListSortDirection.Ascending));
    }
    public ObservableCollection<Layers> layers { get; }
}
class Layers
{
    public Layers(string name, string color)
    {
        Name = name;
        Color = color;
        CollectionViewSource.GetDefaultView(effects).SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
    }
    public string Name { get; set; }
    public string Color { get; set; }
    public ObservableCollection<Effects> effects { get; }
}
class Effects
{
    public string Name { get; set; }
}

之后,您现有的绑定应该可以进行排序。无需更改您的XAML或其他任何内容。