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

发布于 2024-11-02 12:07:37 字数 1030 浏览 0 评论 0原文

我有一个绑定到 TreeViewLayers 列表,其中每个实例都有一个 Effects 列表。我通过 HierarchicalDataTemplate 展示它们,该模板效果很好,但我尝试使用 SortDescriptions 对它们进行排序。

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

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

我尝试首先按 .Color 对它们进行排序,然后按 对它们进行排序.名称

有什么想法吗?

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

<Window.Resources>

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

</Window.Resources>

但这仍然只适用于层次结构的第一级。如何为每个layer.Effects集合指定它?

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 SortDescription ( "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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巴黎盛开的樱花 2024-11-09 12:07:38

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

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

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;
    }
}
红玫瑰 2024-11-09 12:07:38

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

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

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();
        }
    }
}
沉睡月亮 2024-11-09 12:07:38

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

我假设您有如下 3 个类:AllLayers、Layers 和 Layers。效果

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 或其他任何内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文