I am using CollectionViewSource's groupings to display a virtual "aggregate" view of a collection of details; basically the sample seen here: ListBox Grouping, Sorting, Subtotals and Collapsible Regions
To summarize the relevant part of the example, your groupings can expose an aggregated property from their details via a ValueConverter, like so:
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Grid>
<!--[..Snip..]-->
<TextBlock Grid.Column="4"
Text="{Binding StringFormat=\{0:C\},
Converter={StaticResource groupItemSalesSubtotalConverter}}"
TextAlignment="Right" />
<!--[..Snip..]-->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListBox.GroupStyle>
Note that the TextBlock above is bound directly to the GroupItem's binding, so the converter receives the entire CollectionViewGroup
and its children and can drill down into the details to sum the subtotal of the items in the group.
The problem is, since this value isn't itself a property, is it possible to sort the groups by their Subtotal?
This obviously can't be handled by SortDescription
. I've seen suggestions that ListCollectionView's CustomSort
property, which takes an IComparer
implementation can be used in a situation like this, but I don't see how: The Compare()
method receives pairs of detail items regardless of their grouping, I can't determine the subtotals at that point.
Is there any way I can sort these groups without needing to make them entities in their own right (i.e. have a CollectionView at the summary level and another CollectionView at the detail level)?