Как отсортировать элементы TreeView, используя SortDescription в 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>
Но это все еще только для первого уровня иерархии. Как я могу указать его для каждого слоя. Влияет на коллекцию?
2 ответа
Я бы предложил использовать конвертер для сортировки подпунктов. Что-то вроде этого:
<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;
}
}
Я считаю, что использование мульти конвертера лучше:
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();
}
}
}
Это не решение на основе XAML, но я столкнулся с той же проблемой и нашел решение, как показано ниже.
Я предполагаю, что у вас есть 3 класса, как показано ниже: 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; }
}
После этого ваша существующая привязка должна работать с сортировкой. Нет необходимости вносить изменения в ваш XAML или что-то еще.