Как отсортировать элементы TreeView с помощью SortDescriptions в Xaml?

#c# #.net #wpf #xaml #sorting

#c# #.net #wpf #xaml #сортировка

Вопрос:

У меня есть список, 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>
  

Но это все равно делает это только для первого уровня иерархии. Как я могу указать это для каждого слоя?Коллекция эффектов?

Ответ №1:

Я бы предложил использовать converter для сортировки вложенных элементов. Что-то вроде этого:

 <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. Просто очень небольшое улучшение, измените значение как System. Коллекции. Я перечисляю в (System. Коллекции. Значение IList, чтобы избежать исключения ссылки null, если значение не является IList (у вас должно быть исключение InvalidCastException)

Ответ №2:

Я считаю, что лучше использовать мультиконвертер:

 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 amp;amp; 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 Resu<
            }
            return null;
        }

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

Комментарии:

1. чем это лучше? и можете ли вы показать xaml, как это использовать?

Ответ №3:

Это не решение на основе XAML, но я столкнулся с той же проблемой и нашел решение, подобное приведенному ниже,

Я предполагаю, что у вас есть 3 класса, как показано ниже: AllLayers, Layers amp; 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 или что-либо еще.