如何在没有扩展器的情况下对数据网格进行分组

use*_*135 2 wpf xaml grouping expander wpfdatagrid

我正在MSDN 上使用 Datagrid 分组示例。示例中的代码使用 Expander 来显示组的子行。我不想在我的代码中使用扩展器。我想始终显示每一行。如何在不使用 Expander 控件的情况下显示分组数据网格中的子行?

kma*_*zek 5

您可以使用边框代替扩展器。

 <DataGrid.GroupStyle>
                <!-- Style for groups at top level. -->
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Margin" Value="0,0,0,0"/>                            
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">   
                                        <Border BorderThickness="1" BorderBrush="Black" CornerRadius="5,5,5,5" Margin="0,0,0,5">
                                            <StackPanel>
                                                <StackPanel Height="30" Orientation="Horizontal">
                                                    <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0" Width="100" VerticalAlignment="Center"/>
                                                    <TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}" VerticalAlignment="Center" />
                                                </StackPanel>

                                                <ItemsPresenter />
                                            </StackPanel>
                                        </Border>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
                <!-- Style for groups under the top level. -->
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <DockPanel Background="LightBlue">
                                <TextBlock Text="{Binding Path=Name, Converter={StaticResource completeConverter}}" Foreground="Blue" Margin="30,0,0,0" Width="100"/>
                                <TextBlock Text="{Binding Path=ItemCount}" Foreground="Blue"/>
                            </DockPanel>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
 </DataGrid.GroupStyle>
Run Code Online (Sandbox Code Playgroud)