在DataGrid中对数据进行分组能辨析数据的内部信息,反映数据间的依存关系。本文将为大家介绍如何在DataGrid中实施数据的分组。
需要了解的知识
1)PagedCollectionView
它代表了一个有关分组、排序、筛选和导航的分页数据集合。
2)PagedCollectionView的GroupDescriptions属性
对分组信息的描述,用于绑定需要分组的字段。
3)DataGrid的RowGroupHeaderStyles属性
用于设置分组标签头的样式。
实例
详细的说明在代码注释中给出。
MainPage.xaml文件代码:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverlightClient.MainPage"
d:DesignWidth="320" d:DesignHeight="480">
<Grid x:Name="LayoutRoot" Background="White" Width="320" Height="480">
<data:DataGrid x:Name="dgEmployee" Margin="8,8,8,52" FontSize="14" AutoGenerateColumns="False">
<data:DataGrid.RowGroupHeaderStyles>
<!-- 顶层分组样式 -->
<Style TargetType="data:DataGridRowGroupHeader">
<Setter Property="PropertyNameVisibility" Value="Collapsed" />
<Setter Property="Background" Value="#99225566" />
<Setter Property="Foreground" Value="White" />
<Setter Property="SublevelIndent" Value="15" />
</Style>
<!-- 顶层以下分组样式 -->
<Style TargetType="data:DataGridRowGroupHeader">
<Setter Property="Background" Value="#44225566" />
</Style>
</data:DataGrid.RowGroupHeaderStyles>
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="工号" Binding="{Binding EmployeeID}"/>
<data:DataGridTextColumn Header="姓名" Binding="{Binding EmployeeName}"/>
<data:DataGridTextColumn Header="年龄" Binding="{Binding EmployeeAge}"/>
<data:DataGridTextColumn Header="部门" Binding="{Binding DepartmentName}"/>
<data:DataGridTextColumn Header="级别" Binding="{Binding Level}"/>
</data:DataGrid.Columns>
</data:DataGrid>
</Grid>
</UserControl>
MainPage.xaml.cs文件代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.ComponentModel;
namespace SilverlightClient
{
public class Employees
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
public int EmployeeAge { get; set; }
public string DepartmentName { get; set; }
public string Level { get; set; }
}
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
public List<Employees> GetEmployees()
{
List<Employees> returnedValue = new List<Employees>();
returnedValue.Add(new Employees() { EmployeeID = 1, EmployeeName = "张三", EmployeeAge = 23, DepartmentName = "财务部", Level = "初级" });
returnedValue.Add(new Employees() { EmployeeID = 2, EmployeeName = "李四", EmployeeAge = 24, DepartmentName = "财务部", Level = "中级" });
returnedValue.Add(new Employees() { EmployeeID = 3, EmployeeName = "王五", EmployeeAge = 25, DepartmentName = "管理部", Level = "中级" });
returnedValue.Add(new Employees() { EmployeeID = 4, EmployeeName = "赵六", EmployeeAge = 26, DepartmentName = "管理部", Level = "初级" });
returnedValue.Add(new Employees() { EmployeeID = 5, EmployeeName = "钱七", EmployeeAge = 27, DepartmentName = "工程部", Level = "中级" });
returnedValue.Add(new Employees() { EmployeeID = 6, EmployeeName = "孙八", EmployeeAge = 28, DepartmentName = "工程部", Level = "初级" });
returnedValue.Add(new Employees() { EmployeeID = 7, EmployeeName = "小赵", EmployeeAge = 26, DepartmentName = "财务部", Level = "中级" });
returnedValue.Add(new Employees() { EmployeeID = 8, EmployeeName = "小钱", EmployeeAge = 28, DepartmentName = "财务部", Level = "高级" });
return returnedValue;
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
PagedCollectionView pcv = new PagedCollectionView(GetEmployees());
pcv.GroupDescriptions.Add(new PropertyGroupDescription("DepartmentName"));//添加分组信息描述
pcv.GroupDescriptions.Add(new PropertyGroupDescription("Level"));
dgEmployee.ItemsSource = pcv;
}
}
}
最终效果图
文章出处:Kinglee’s Blog ( http://www.cnblogs.com/Kinglee/)
版权声明:本文的版权归作者与博客园共有。转载时须注明本文的详细链接,否则作者将保留追究其法律责任。