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

时间:2016-08-03 08:17:35

标签: c# .net wpf vb.net mvvm

我更喜欢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

我的观点的代码如下:

<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文件:

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。

这是随机值的样子:

Prototype

我的方法

我通过跟随调试器来到达单元格,因此我执行了以下操作:

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

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

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

groupTypeInacessable

1 个答案:

答案 0 :(得分:2)

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

更新TextBlock中的Expander.Header以使用此Converter

<TextBlock Text="{Binding ItemCount}" .../>中的Expander Header更改为<TextBlock Text="{Binding .,Converter={StaticResource SumCnvKey}}" .../>

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

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();
    }
}

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

Group Sum Output

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