原文:C# WPF DataGrid 分组(Group)

效果如图,每个列的名字可以自定义。我随便用了”File”和”Attachment Name”。 

在Window的Resources里面设置Style, GroupHeaderStyle:

        <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True">
<Expander.Header>
<TextBlock Text="{Binding Path=Name}"/>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

把这个Style应用到DataGrid上面:

            <DataGrid.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
<GroupStyle.Panel>
<ItemsPanelTemplate>
<DataGridRowsPresenter/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</DataGrid.GroupStyle>

整体xaml文件:

<Window x:Class="DataGridGroupDeamon.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True">
<Expander.Header>
<TextBlock Text="{Binding Path=Name}"/>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<DataGrid x:Name="dataGrid1" AutoGenerateColumns="False" Margin="10">
<DataGrid.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
<GroupStyle.Panel>
<ItemsPanelTemplate>
<DataGridRowsPresenter/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</DataGrid.GroupStyle> <DataGrid.Columns>
<DataGridTextColumn Header="File" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Attachment Name" Binding="{Binding Sex}"/> </DataGrid.Columns>
</DataGrid>
</Grid>
</Window>

数据准备文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel; namespace DataGridGroupDeamon
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
ObservableCollection<Model> data = new ObservableCollection<Model>(); public MainWindow()
{
InitializeComponent(); dataGrid1.ItemsSource = data;
for (int i = 0; i < 10; i++)
{
Model m = CreateModel("张" + i, i / 2 + "年级", (i % 2 == 0 ? "女" : "男"));
data.Add(m);
}
ICollectionView vw = CollectionViewSource.GetDefaultView(data);
vw.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
} private Model CreateModel(string name, string cate, string sex)
{
Model model = new Model();
model.Name = name;
model.Category = cate;
model.Sex = sex;
return model;
}
} public class Model
{
public string Name
{
get;
set;
} public string Category
{
get;
set;
} public string Sex
{
get;
set;
}
}
}

C# WPF DataGrid 分组(Group)的更多相关文章

  1. WPF DataGrid分组和排序

    之前一直用的Dev的GridControl,控件自带分组排序啥的.今天试了下在wpf自带的Datagrid控件上实现分组和排序. Datagrid上实现这些功能主要用到CollectionViewSo ...

  2. WPF DataGrid 分组

    public ListCollectionView collection; collection = new ListCollectionView(obj.empData); collection.G ...

  3. WPF DataGrid常用属性记录

    WPF DataGrid常用属性记录 组件常用方法: BeginEdit:使DataGrid进入编辑状态. CancelEdit:取消DataGrid的编辑状态. CollapseRowGroup:闭 ...

  4. xceed wpf datagrid

    <!--*********************************************************************************** Extended ...

  5. C# WPF DataGrid控件实现三级联动

    利用DataGrid控件实现联动的功能,在数据库客户软件中是随处可见的,然而网上的资料却是少之又少,令人崩溃. 本篇博文将介绍利用DataGrid控件模板定义的三个ComboBox实现“省.市.区”的 ...

  6. Oracle 表分组 group by和模糊查询like

    分组group by写法 select 字段名 from 表名 group by 字段名 查询这个字段名里的种类分组后可以加聚合函数select 字段名,聚合函数 from 表名 group by 字 ...

  7. WPF DATAGRID - COMMITTING CHANGES CELL-BY-CELL

    In my recent codeproject article on the DataGrid I described a number of techniques for handling the ...

  8. WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.

    WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次  悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ...

  9. 获取wpf datagrid当前被编辑单元格的内容

    原文 获取wpf datagrid当前被编辑单元格的内容 确认修改单元个的值, 使用到datagrid的两个事件 开始编辑事件 BeginningEdit="dataGrid_Beginni ...

随机推荐

  1. 微软Azure AspNetCore微服务实战 第二期

    2018新年快乐!继第1期微服务技术交流活动后(http://www.huodongxing.com/event/4403161781600),本次我们将与上海的小伙伴们继续深入分享微服务技术的应用实 ...

  2. Loadrunner 脚本录制-通过代理录制脚本

    脚本录制-通过代理录制脚本 by:授客 QQ:1033553122 版本:Loadruner 11.0 A.PC端录制Web应用程序 步骤1:根据实际情况,选择对应的协议 本例中选择Web(HTTP/ ...

  3. 简述 Spring Cloud 是什么1

    很多同学都了解了Spring ,了解了 Spring Boot, 但对于 Spring Cloud 是什么还是比较懵逼的. 本文带你简单的了解下,什么是Spring Cloud. Spring Clo ...

  4. [20171120]关于find 软连接问题.txt

    [20171120]关于find 软连接问题.txt --//上个星期为了测试oracle参数filesystemio_options,将数据库做了一次移动.但是我使用find对软链接目录查询时--/ ...

  5. 【第六篇】SAP ABAP7.5x新语法之SQL注入

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:SAP ABAP7.5x系列之SQL注入   前 ...

  6. 针对需要使用T3协议的Weblogic2628漏洞解决方案

    针对需要使用T3协议的Weblogic2628漏洞解决方案 前几天用户的服务器中检查到了Weblogic2628l漏洞,并且打过Oracle官方补丁后还是能检测到. 针对此问题,去网上查找了一些资料. ...

  7. Go语言学习笔记(一)Let's 干吧

    加 Golang学习 QQ群共同学习进步成家立业工作 ^-^ 群号:96933959 简介     Go是Google开发的一种 静态强类型.编译型,并发型,并具有垃圾回收功能的编程语言.为了方便搜索 ...

  8. window下上传文件至linux(windows下如何访问linux)

    ========相信我,按照步骤来一定能成功====== 我将从三个方面来说明:为什么要搭建访问服务器.如何搭建访问服务器.windows如下访问 为什么要搭建访问Linux服务器 我们都知道,服务器 ...

  9. Linux 小知识翻译 - 「文件系统的种类」

    现在的Linux,主流的文件系统是 「ext3」.但是,文件系统除此之外,还有「ReiserFS」「XFS」「ZFS」等等. 此外,Windows的主流文件系统是「NTFS」,CD-ROM的主流文件系 ...

  10. 第一章 Bootstrasp起步

    模板如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf ...