C#WPF工具包DataGrid - Group里面的组?

时间:2011-03-10 22:51:37

标签: c# wpf datagrid toolkit

我有一个组中有项目,我希望该组有一个子组。

主要群组:“Windows”,“Linux”,“Mac”和子群组:“数据”,“基本”,“高级”

子组将包含行项。此外,每个主要组可以有不同的子组。

我该怎么做?

以下是我的一些代码:

<Window.Resources>
    <CollectionViewSource x:Key="OSGroups">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="os"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

    <Style x:Key="GroupContainerStyle" TargetType="{x:Type GroupItem}">
        <Style.Resources>
            <LinearGradientBrush x:Key="OutBrush" StartPoint="0,0" EndPoint="0,1">
                <GradientStop Offset="0" Color="#FEB74B"/>
                <GradientStop Offset="1" Color="#F19201"/>
            </LinearGradientBrush>
        </Style.Resources>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupItem}">
                    <Expander IsExpanded="True" Background="{StaticResource OutBrush}" BorderThickness="0">
                        <Expander.Header>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>

                                <StackPanel Grid.Column="0" Orientation="Horizontal" Margin="0,0,10,0">
                                    <TextBlock Text="{Binding Path=Name}"/>
                                </StackPanel>
                            </Grid>
                        </Expander.Header>
                        <ItemsPresenter />
                    </Expander>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <wpfToolkit:DataGrid Name="CommandsDataGrid" AutoGenerateColumns="False" IsReadOnly="True" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False" CanUserSortColumns="False" ItemsSource="{Binding Source={StaticResource OSGroups}}" CellStyle="{StaticResource CellStyle}" HeadersVisibility="Column" Height="250" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <wpfToolkit:DataGrid.GroupStyle>
            <GroupStyle ContainerStyle="{StaticResource GroupContainerStyle}">
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <wpfToolkit:DataGridRowsPresenter/>
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
            </GroupStyle>
        </wpfToolkit:DataGrid.GroupStyle>
        <wpfToolkit:DataGrid.Columns>
            <wpfToolkit:DataGridTextColumn Header="Command" Binding="{Binding Path=command}" />
            <wpfToolkit:DataGridTextColumn Header="Description" Width="*" Binding="{Binding Path=description}" />
        </wpfToolkit:DataGrid.Columns>
    </wpfToolkit:DataGrid>
</Grid>

2 个答案:

答案 0 :(得分:1)

请注意,缩进仅适用于一个子组,因为我们只有“IsBottomLevel”而不是“Level”。

    <CollectionViewSource x:Key="OSGroups">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="os"/>
            <PropertyGroupDescription PropertyName="subGroup"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
    <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupItem}">
                    <Expander IsExpanded="True">
                        <Expander.Style>
                            <Style TargetType="{x:Type Expander}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsBottomLevel}" Value="True">
                                        <Setter Property="Margin" Value="20,0,0,0" />
                                    </DataTrigger>

                                </Style.Triggers>
                            </Style>
                        </Expander.Style>
                        <Expander.Header>
                            <TextBlock Text="{Binding Name}"/>
                        </Expander.Header>
                        <ItemsPresenter Margin="-10,0,0,0" />
                    </Expander>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

答案 1 :(得分:0)

您可以在DataGrid.GroupStyle元素中定义任何样式。考虑下面的xaml,这里我定义了两个组容器样式,即GroupContainerStyleSecondGroupContainerStyle。现在主要组(第1级)GroupConstainerStyle将被应用,并且对于您的子组(第2级)SecondGroupContainerStyle将自动应用。

SecondGroupContainerStyle中你可以改变缩进,就像spasrto一样。

<wpfToolkit:DataGrid Name="CommandsDataGrid" AutoGenerateColumns="False" IsReadOnly="True" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False" CanUserSortColumns="False" ItemsSource="{Binding Source={StaticResource OSGroups}}" CellStyle="{StaticResource CellStyle}" HeadersVisibility="Column" Height="250" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <wpfToolkit:DataGrid.GroupStyle>
        <GroupStyle ContainerStyle="{StaticResource GroupContainerStyle}">
            <GroupStyle.Panel>
                <ItemsPanelTemplate>
                    <wpfToolkit:DataGridRowsPresenter/>
                </ItemsPanelTemplate>
            </GroupStyle.Panel>
        </GroupStyle>

        <GroupStyle ContainerStyle="{StaticResource SecondGroupContainerStyle}">
            <GroupStyle.Panel>
                <ItemsPanelTemplate>
                    <wpfToolkit:DataGridRowsPresenter/>
                </ItemsPanelTemplate>
            </GroupStyle.Panel>
        </GroupStyle>           
    </wpfToolkit:DataGrid.GroupStyle>

</wpfToolkit:DataGrid>