ICollectionView 中组内的项目未排序

Items inside Groups in ICollectionView are not sorted

我有一个 ObservableCollection<string>:

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

Window 中的 ListBox 绑定到此集合。在 Window Loaded 事件中,我将排序和分组逻辑分配给 Collection 的基础 ICollectionView。

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 returns 转换方法中字符串的长度。

结果如下:

我希望组按升序排序,其中的项目按降序排序。但似乎没有使用组内项目的 SortDescription — 它没有按降序排序。

我不确定我做错了什么。

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

排序
defaultView.SortDescriptions.Add(new SortDescription("Length", ListSortDirection.Ascending));
defaultView.SortDescriptions.Add(new SortDescription("", ListSortDirection.Descending));