开发者

How to sort TreeView items using SortDescriptions in Xaml?

开发者 https://www.devze.com 2023-02-26 21:14 出处:网络
I have a list of Layers binded to a TreeView where each instance has a list of Effects. I show them via a HierarchicalDataTemplate which works great but I am trying to sort them using SortDescriptions

I have a list of Layers binded to a TreeView where each instance has a list of Effects. I show them via a HierarchicalDataTemplate which works great but I am trying to sort them using SortDescriptions.

I don't know how to do this in xaml but doing this sorts only the first level of items, not the sub items:

ICollectionView view = CollectionViewSource.GetDefaultView ( treeView1.ItemsSource );
view.SortDescriptions.Add ( new SortDescr开发者_运维知识库iption ( "Name", ListSortDirection.Ascending ) );

I am trying to sort them first by .Color, then by .Name.

Any ideas?

EDIT: I added this code:

<Window.Resources>

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

</Window.Resources>

But this still only does it for the first level of hierarchy. How can I specify it for each layer.Effects collection?


I would suggest to use converter to sort sub items. Something like this:

<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>

and converter:


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


I find using a multi converter is better:

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();
        }
    }
}


This is not the XAML based solution, but I was facing the same problem and found the solution like below,

I am assuming you have 3 classes like below: AllLayers, Layers & 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; }
}

After that, your existing binding should work with sorting. No need of change to your XAML or anything.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号