如何在分组CollectionView中获取单元格的值?

我更喜欢VB.NET中的解决方案,但C#也非常受欢迎。

我有一个ObservableCollection我分组。 这些组可以展开和折叠。 每个组具有不同数量的具有多个单元格的行。 我想对每个组的每一行的单个单元格的值求和。 这是为了你的想象力,以便你可以跟随我的想法。

A组总数:“Cell#0 = 55” – “Cell#1 = 70”

Row#0 |-- Cell#0 = 25 --|-- Cell#1 = 50 --| Row#1 |-- Cell#0 = 35 --|-- Cell#1 = 20 --| 

B组总金额:0

C组总量:“Cell#0 = 12” – “Cell#1 = 8”

 Row#0 |-- Cell#0 = 12 --|-- Cell#1 = 8 --| 

D组总量:“Cell#0 = 150” – “Cell#1 = 99”

 Row#0 |-- Cell#0 = 25 --|-- Cell#1 = 33 --| Row#1 |-- Cell#0 = 75 --|-- Cell#1 = 33 --| Row#2 |-- Cell#0 = 50 --|-- Cell#1 = 33 --| 

这是一个简短的代码示例,以便您可以自己尝试。

我有一个类库,我称之为模型。 该库包含一个名为ProjectCompletedModel.vb的类

 Public Class ProjectCompletedModel Public Property SomeColumnToGroup As String Public Property SomeValueToSum As Double End Class 

接下来我有一个类库libModels。 它包含类ProjectCompletedViewModel.vb

 Public Class ProjectCompletedViewModel Private _projCompl As New ProjectCompletedModel Public Property Projects As ObservableCollection(Of ProjectCompletedModel) Public Property SomeColumnToGroup As String Get Return Me._projCompl.SomeColumnToGroup End Get Set(value As String) Me._projCompl.SomeColumnToGroup = value End Set End Property Public Property SomeValueToSum As Double Get Return Me._projCompl.SomeValueToSum End Get Set(value As Double) Me._projCompl.SomeValueToSum = value End Set End Property Public Sub New() Me.SetProjects() Dim view As CollectionView = CollectionViewSource.GetDefaultView(Me.Projects) view.GroupDescriptions.Clear() view.GroupDescriptions.Add(New PropertyGroupDescription("SomeColumnToGroup")) view.SortDescriptions.Add(New SortDescription("SomeColumnToGroup", ListSortDirection.Ascending)) End Sub End Class 

我的观点的代码如下:

                                               

最后是.xaml.vb文件:

 Public Class ProjectCompletedView Public Sub New() ' This call is required by the designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. Me.DataContext = New ProjectCompletedViewModel End Sub End Class 

我的应用程序引用了ViewModels。 ViewModels to Models。

这是随机值的样子:

原型

我的方法

我通过跟随调试器来到达单元格,所以我做了以下事情:

ProjectCompletedModel.vb在构造函数中进行分组。 在那里,我想获得GroupNames。

  • 法拉利
  • 兰博基尼
  • 莲花
  • 等等

对于每个GroupName,我想循环遍历我的ObservAbleCollection并查看“ColumnName”是否与“GroupName”匹配。 然后访问此条件为真的行的单元格。 但我甚至无法获得GroupNames,因为内部实现属于这种类型,我无法检索任何信息。

groupTypeInacessable

编写Converter (eg; SumConverter)来计算值的总和。

更新Expander.HeaderTextBlock以使用此Converter

Expander Header 更改为

现在,将为每个组调用您的SumConverter。

 public class SumConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { CollectionViewGroup group = (CollectionViewGroup)value; ReadOnlyObservableCollection items = group.Items; var sum = (from p in items select ((ProjectCompletedModel)p).SomeValueToSum).Sum(); return sum; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } 

我在一个集合中创建了自己的带有CarViewModel ,并进行了检查,它运行正常。

组总和输出

看看这是否解决了您的问题。