为其他GroupDescriptions缩进DataGrid.GroupStyle

时间:2011-07-17 18:43:26

标签: wpf grouping wpfdatagrid converter

我正在使用ListCollectionView作为WPF DataGrid的ItemsSource。

我希望用户能够将列添加到group by,并且我使用以下作为GroupStyle:

<DataGrid.GroupStyle>
    <GroupStyle>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <Expander>
                                <Expander.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="Group Name: "/>
                                        <TextBlock Text="{Binding Path=Name}" FontWeight="Bold"/>
                                        <TextBlock Text=" "/>
                                        <TextBlock Text="{Binding Path=ItemCount}" FontStyle="Italic"/>
                                        <TextBlock Text=" Items" FontStyle="Italic"/>
                                    </StackPanel>
                                </Expander.Header>
                                <ItemsPresenter>

                                </ItemsPresenter>
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</DataGrid.GroupStyle>

我的问题是,当GroupDescriptions中有超过1列时,则显示组标题而不缩进。另一个问题是,我希望将具有“组名:”的TextBox绑定到对该级别进行分组的列名称 - 所以如果我在group = Gender上的groupint,则会说“性别:”。< / p>

那么如何根据GroupDescriptions集合中的嵌套级别缩进组标题,以及如何绑定到列名?

1 个答案:

答案 0 :(得分:3)

没有人加强到这一点,所以经过大量的修补,我提出了以下解决方案。我创建了一个多值转换器,将当前的CollectionViewGroup,整个ListCollectionView和DataGrid作为参数。

Public Class GroupLevelConverter
    Implements IMultiValueConverter

    Public Function Convert(values() As Object, targetType As System.Type,
                            parameter As Object, culture As System.Globalization.CultureInfo
                            ) As Object Implements System.Windows.Data.IMultiValueConverter.Convert
        If TypeOf values(0) Is CollectionViewGroup Then
            Dim level As Integer = 0
            Dim parent As CollectionViewGroup = values(0)
            Do While parent IsNot Nothing
                parent = GetParent(values(0))
                values(0) = parent
                If parent IsNot Nothing Then
                    level += 1
                End If
            Loop
            Dim s As String = ""
            For i = 1 To level - 1
                s += "    "
            Next
            Dim lcv As ListCollectionView = DirectCast(values(1), ListCollectionView)
            Dim pgd As System.Windows.Data.PropertyGroupDescription = lcv.GroupDescriptions(level - 1)
            Dim dg As DataGrid = values(2)
            Dim GroupHeader As String = pgd.PropertyName
            For c = 0 To dg.Columns.Count - 1
                If dg.Columns(c).SortMemberPath = GroupHeader Then
                    GroupHeader = dg.Columns(c).Header
                    Exit For
                End If
            Next
            s = s & GroupHeader & ": "
            Return (s)
        End If
        Return ""

    End Function

    Public Function ConvertBack(value As Object, targetTypes() As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack
        Throw New NotSupportedException("Not implemented")
    End Function

    Friend Function GetParent(currentViewGroup As CollectionViewGroup) As CollectionViewGroup
        Dim parent As CollectionViewGroup
        Try
            parent = TryCast(currentViewGroup.[GetType]().GetProperty("Parent", System.Reflection.BindingFlags.GetProperty Or
                                                                                                System.Reflection.BindingFlags.Instance Or
                                                                                                System.Reflection.BindingFlags.NonPublic).GetValue(currentViewGroup, Nothing), 
                                                                  CollectionViewGroup)
        Catch ex As Exception
            Return Nothing
        End Try
        Return parent
    End Function
End Class

CollectionViewGroup用于通过GetParent函数查找其父级。这是解决方案的非优雅部分,因为它依赖于捕获错误。调用该函数,直到错误指示到达顶层组。

ListCollectionView用于获取排序列,而DataGrid用于获取更友好的列标题。

HTH有人在看类似的问题。