1

我有一个ObservableCollection<string>

public ObservableCollection<string> Collection { get; set; } = new ObservableCollection<string>
{
    "AA",
    "BB",
    "CC",
    "C",
    "A",
    "C",
    "BBB",
    "AAA",
    "CCC"
};

Window 中的AListBox绑定到此 Collection。在 Window Loaded 事件中,我将排序和分组逻辑分配给Collection.

private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
    ICollectionView defaultView = CollectionViewSource.GetDefaultView(this.Collection);
    defaultView.GroupDescriptions.Add(new PropertyGroupDescription(null, new TestGroupConverter()));

    defaultView.SortDescriptions.Add(new SortDescription("", ListSortDirection.Ascending));
    defaultView.SortDescriptions.Add(new SortDescription("", ListSortDirection.Descending));
}

TestGroupConverter在其转换方法中返回字符串的长度。

结果如下:

在此处输入图像描述

我希望这些组按升序排序,其中的项目按降序排序。但似乎SortDescription未使用组内的 for 项目 - 它未按降序排序。

我不确定我做错了什么。

4

1 回答 1

3

即使您使用分组并且您有两个具有相同属性的排序描述,所有排序描述也适用于项目。不幸的是,您不能按转换后的值排序,但您可以先更改SortDescription为按Length属性排序

defaultView.SortDescriptions.Add(new SortDescription("Length", ListSortDirection.Ascending));
defaultView.SortDescriptions.Add(new SortDescription("", ListSortDirection.Descending));
于 2016-04-16T20:33:58.833 回答