I have an ObservableCollection<string>
:
public ObservableCollection<string> Collection { get; set; } = new ObservableCollection<string>
{
"AA",
"BB",
"CC",
"C",
"A",
"C",
"BBB",
"AAA",
"CCC"
};
A ListBox
in the Window binds to this Collection. In the Window Loaded event, I am assigning sorting and grouping logic to the underlying ICollectionView of the 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
returns the length of the string in its convert method.
So here's the result:
I expected the groups to be sorted in ascending order and items within it to be sorted by descending order. But it appears that the SortDescription
for items within the group is not used — It's not sorted in Descending order.
I'm not sure what I am doing wrong.
All sort descriptions apply to items even when you use grouping and you have two sort descriptions with the same property. Unfortunately you cannot sort by converted value but you can change first SortDescription
to sort by Length
property
defaultView.SortDescriptions.Add(new SortDescription("Length", ListSortDirection.Ascending));
defaultView.SortDescriptions.Add(new SortDescription("", ListSortDirection.Descending));