【问题标题】:How to Group a Datagrid without an Expander如何在没有扩展器的情况下对数据网格进行分组
【发布时间】:2012-12-04 17:58:43
【问题描述】:

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

【问题讨论】:

    标签: wpf xaml wpfdatagrid grouping expander


    【解决方案1】:

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

     <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>
    

    【讨论】:

      猜你喜欢
      • 2013-10-16
      • 2022-01-25
      • 2014-03-03
      • 2020-10-25
      • 2011-08-01
      • 1970-01-01
      • 2010-12-10
      • 2014-12-07
      • 1970-01-01
      相关资源
      最近更新 更多