如何使用SortDescriptions在XAML TreeView控件项目进行排序? [英] How to sort TreeView items using SortDescriptions in Xaml?

查看:467
本文介绍了如何使用SortDescriptions在XAML TreeView控件项目进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的图层列表绑定到一个的TreeView ,其中每个实例有<$ C $名单C>效果。我通过HierarchicalDataTemplate伟大的工程给他们,但我想用对它们进行排序 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.

我不知道如何做到这一点在XAML中,但这样做排序的项目只有第一级别的,而不是分项:

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

我想对它们进行排序首先由。颜色,然后通过 .Name点

任何想法?

编辑:我添加了这个$​​ C $ C:

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>

但是,这仍然仅会为层次结构的第一级。我怎么可以指定它为每个layer.Effects收藏?

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>

和转换器:


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

这篇关于如何使用SortDescriptions在XAML TreeView控件项目进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆