根据 IValueConverter 生成的属性对 CollectionView 的组进行排序

发布于 2024-10-25 15:40:59 字数 1840 浏览 2 评论 0原文

我正在使用 CollectionViewSource 的分组来显示详细信息集合的虚拟“聚合”视图;基本上是这里看到的示例: ListBox 分组、排序、小计和可折叠区域

要总结示例的相关部分,您的分组可以通过 ValueConverter 从其详细信息中公开聚合属性,如下所示:

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

请注意,上面的 TextBlock 是直接绑定到 GroupItem 的绑定,因此转换器会接收整个 CollectionViewGroup 及其子项,并可以深入了解详细信息以对组中项目的小计进行求和。

问题是,由于该值本身不是属性,是否可以按小计对组进行排序?

这显然无法通过 SortDescription 处理。我看到了 ListCollectionView 的 CustomSort 属性的建议,该属性采用 IComparer 实现 可以在这样的情况下使用,但我不知道如何使用:Compare() 方法接收详细信息对无论项目如何分组,我当时都无法确定小计。

有什么方法可以对这些组进行排序,而无需将它们设置为实体(即在摘要级别有一个 CollectionView,在详细级别有另一个 CollectionView)?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九局 2024-11-01 15:41:08

我相信 ItemsControl 中显示的组的顺序是由集合的项目排序顺序决定的。

因此,通常情况下,您将首先按分组属性排序,然后按您想要在组内实现的顺序排序...

在您的情况下,您想按另一个属性对组进行排序(即,如果该组中的项目的总和)团体)。我知道轻松做到这一点的唯一方法是向项目添加一个新属性,该属性返回每个组的总和(或基于所需顺序的任意排序键)。然后,您可以按该属性对集合进行排序,这将为您提供所需的组排序。

这看起来不太高效或优雅,因为您需要设置/计算集合中每个项目的组总和,实际上是在代码中计算分组。但是,我不知道更好的方法。

I believe the order of the groups shown in the ItemsControl is determined by the collection's items sort order.

So, normally, you would sort first by the grouping property, and then second by the order you want to achieve within the group...

In your case you want to sort the groups by another property (i.e. the sum if the items in that group). The only way I know to do this easily is to add a new property to the item, which returns the sum of each group (or an arbitrary sort key, based on the required order). You can then sort the collection by that property, which will give you the required group ordering.

This doesn't seem terribly efficient or elegant, because you need to set/calculate the sum of the group for each item in the collection, in effect calculating the grouping in code. However, I don't know of a better way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文