我正在使用 Datagrid 分组示例 here at MSDN 。示例中的代码使用 Expander 来显示组的子行。我不想在我的代码中使用扩展器。我想始终显示每一行。如何在不使用 Expander 控件的情况下显示分组数据网格中的子行?
最佳答案
您可以使用边框代替扩展器。
<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>
关于wpf - 如何在没有扩展器的情况下对数据网格进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13917552/