DataGrid分组显示数据

时间:2022-08-08 16:21:41

代码说明使用RowGroupHeaderStyles属性为分组数组样式

 

DataGrid分组显示数据DataGrid分组显示数据View Code
 1 <Grid x:Name="LayoutRoot" Background="White" Width="433" Height="476">
2
3 <sdk:DataGrid x:Name="dgEmployee" Margin="1,3,1,130" FontSize="14" AutoGenerateColumns="False">
4 <sdk:DataGrid.RowGroupHeaderStyles>
5 <!-- 顶层分组样式 -->
6 <Style TargetType="sdk:DataGridRowGroupHeader">
7 <Setter Property="PropertyNameVisibility" Value="Collapsed"/>
8 <Setter Property="Background" Value="#99225566"/>
9 <Setter Property="Foreground" Value="White"/>
10 <Setter Property="SublevelIndent" Value="15"/>
11
12 </Style>
13
14 <!-- 顶层以下分组样式 -->
15 <Style TargetType="sdk:DataGridRowGroupHeader">
16 <Setter Property="Background" Value="#44225566"/>
17 </Style>
18 </sdk:DataGrid.RowGroupHeaderStyles>
19 <sdk:DataGrid.Columns>
20 <sdk:DataGridTextColumn Header="工号" Binding="{Binding EmployeeID}" IsReadOnly="True"/>
21 <sdk:DataGridTextColumn Header="姓名" Binding="{Binding EmployeeName}" IsReadOnly="True"/>
22 <sdk:DataGridTextColumn Header="年龄" Binding="{Binding EmployeeAge}"/>
23 <sdk:DataGridTextColumn Header="部门" Binding="{Binding DepartmentName}"/>
24 <sdk:DataGridTextColumn Header="级别" Binding="{Binding Level}"/>
25 </sdk:DataGrid.Columns>
26 </sdk:DataGrid>
27 </Grid>

使用

DataGrid分组显示数据

自带试图PagedCollectionView放置数据源

 

DataGrid分组显示数据DataGrid分组显示数据View Code
1  PagedCollectionView pcv =new PagedCollectionView(GetEmployees());
2 dgEmployee.ItemsSource = pcv;
3 pcv.GroupDescriptions.Add(new PropertyGroupDescription("Level"));//添加分组信息描述
4 foreach (CollectionViewGroup item in pcv.Groups)
5 {
6 dgEmployee.CollapseRowGroup(item, false);
7 }

结果显示

DataGrid分组显示数据