如何在分组的 CollectionView 中获取单元格的值? [英] How do I get the value of a cell in a grouped CollectionView?
问题描述
我更喜欢 VB.NET 中的解决方案,但也非常欢迎 C#.
I would prefer a solution in VB.NET but C# is also very welcome.
我有一个我分组的 ObservableCollection.这些组可以展开和折叠.每个组都有不同数量的行和多个单元格.我想为每组的每一行求和一个单元格的值.这是为了您的想象力,以便您可以遵循我的想法.
I have a ObservableCollection which I group. The groups can be expanded and collapsed. Each group has a different amount of rows with multiple cells. I want to sum the value of a single cell for every row of each group. This is for your imagination so that you can follow my thoughts.
A 组总数:Cell#0 = 55"-Cell#1 = 70"
Group A Total amount: "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"
Group C Total amount: "Cell#0 = 12" - "Cell#1 = 8"
Row#0 |-- Cell#0 = 12 --|-- Cell#1 = 8 --|
D 组总金额:"Cell#0 = 150" - "Cell#1 = 99"
Group D Total amount: "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 --|
这是一个简短的代码示例,您可以根据需要自行尝试.
Here is a short code example so that you can try it out yourself if you want.
我有一个类库,我称之为模型.该库包含一个名为 ProjectCompletedModel.vb
I have a class libary which I call Models. This libary contains a class called ProjectCompletedModel.vb
Public Class ProjectCompletedModel
Public Property SomeColumnToGroup As String
Public Property SomeValueToSum As Double
End Class
接下来我有一个类库 ViewModels.它包含类 ProjectCompletedViewModel.vb
Next I have a class libary ViewModels. It contains the class 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
我的视图代码如下:
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<!-- Define the Grid design -->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="3*"/>
</Grid.RowDefinitions>
<ListView Grid.ColumnSpan="2"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="0,52,0,0"
Grid.RowSpan="2"
ItemsSource="{Binding Projects}">
<!-- Define the column names -->
<ListView.View>
<GridView>
<GridViewColumn Header="SomeColumnToGroup" Width="150" DisplayMemberBinding="{Binding SomeColumnToGroup}"/>
<GridViewColumn Header="SomeValueToSum" Width="100" DisplayMemberBinding="{Binding SomeValueToSum}"/>
</GridView>
</ListView.View>
<!-- Define the group style-->
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="True">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="#FF425C8F" FontSize="14"/>
<TextBlock Text="(" FontSize="14" Foreground="#FF425C8F"/>
<TextBlock Text="{Binding ItemCount}" Foreground="#FF425C8F" FontSize="14" FontWeight="Bold"/>
<TextBlock Text=")" Foreground="#FF425C8F" FontSize="14"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
</Grid>
最后是 .xaml.vb 文件:
Finally the .xaml.vb file:
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
我的应用程序引用了 ViewModel.视图模型到模型.
My application has a reference to ViewModels. ViewModels to Models.
这是随机值的样子:
我的方法:
我通过跟随调试器到达单元格,因此我执行了以下操作:
I managed by following the debugger to reach the cells so I did the following:
ProjectCompletedModel.vb 在构造函数中进行分组.在那里我想获得 GroupNames.
ProjectCompletedModel.vb does the grouping in the constructor. There I wanted to get the GroupNames.
- 法拉利
- 兰博基尼
- 莲花
- 等
对于每个 GroupName,我想遍历我的 ObservAbleCollection 并查看ColumnName"是否与GroupName"匹配.然后访问此条件为真的行的单元格.但我什至无法获得 GroupNames,因为内部实现属于这种类型,我无法检索任何信息.
For each GroupName I wanted to loop through my ObservAbleCollection and see if the "ColumnName" matches the "GroupName". Then access the cell for the row where this condition is true. But I can't even get the GroupNames as the internal implementation is of this type which I can't retrieve any information.
推荐答案
编写一个 Converter (eg; SumConverter)
来计算值的总和.
Write a Converter (eg; SumConverter)
to calculate the sum of the values.
更新 Expander.Header
中的 TextBlock
以使用此 Converter
.
Update your TextBlock
in your Expander.Header
to use this Converter
.
将 <TextBlock Text="{Binding ItemCount}" .../>
在您的 Expander Header
更改为 <TextBlock Text="{绑定 .,Converter={StaticResource SumCnvKey}}" .../>
.
Change <TextBlock Text="{Binding ItemCount}" .../>
in your Expander Header
to <TextBlock Text="{Binding .,Converter={StaticResource SumCnvKey}}" .../>
.
现在将为每个组调用您的 SumConverter.
Now your SumConverter will be called for every group.
public class SumConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
CollectionViewGroup group = (CollectionViewGroup)value;
ReadOnlyObservableCollection<object> 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();
}
}
我在一个集合中用 Car
创建了我自己的 ViewModel
并检查,它工作正常.
I created my own ViewModel
with Car
in a collection, and checked, it works fine.
看看这是否能解决您的问题.
See if this solves your issue.
这篇关于如何在分组的 CollectionView 中获取单元格的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!