WPF DataGrid分组和排序
之前一直用的Dev的GridControl,控件自带分组排序啥的。今天试了下在wpf自带的Datagrid控件上实现分组和排序。

Datagrid上实现这些功能主要用到CollectionViewSource。CollectionViewSource有对数据进行分组和排序过滤的功能。
<CollectionViewSource x:Key="cvsList" Source="{Binding List}"
>
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="B"/>
</CollectionViewSource.GroupDescriptions>
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="B" Direction="Descending"/>
<scm:SortDescription PropertyName="E"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
将数据源绑定到CollectionViewSource,并设置分组列和排序列。倒序可以设置Direction属性为Descending
<DataGrid ItemsSource="{Binding Source={StaticResource cvsList}}" />
将CollectionViewSource绑定到DataGrid,并设置DataGrid的GroupStyle,分组功能就可以使用了。
下面是我的例子
<Window x:Class="DataGridTempalte.MainWindow"
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"
xmlns:local="clr-namespace:DataGridTempalte"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"> <Window.Resources> <CollectionViewSource x:Key="cvsList" Source="{Binding List}"
>
<CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName="B"/>
</CollectionViewSource.GroupDescriptions>
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="B" Direction="Descending"/>
<scm:SortDescription PropertyName="E"/> </CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources> <Grid>
<DataGrid ItemsSource="{Binding Source={StaticResource cvsList}}" AutoGenerateColumns="False">
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0,0,0,5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" Background="#FF112255" BorderBrush="#FF002255" BorderThickness="1,1,1,5"
>
<Expander.Header>
<DockPanel>
<TextBlock Foreground="White" FontWeight="Bold" Text="{Binding Path=Name}"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="A" Binding="{Binding A}"/>
<DataGridTextColumn Header="B" Binding="{Binding B}"/>
<DataGridTextColumn Header="C" Binding="{Binding C}"/>
<DataGridTextColumn Header="D" Binding="{Binding D}"/>
<DataGridTextColumn Header="E" Binding="{Binding E}"/>
</DataGrid.Columns> </DataGrid> </Grid>
</Window>
WPF DataGrid分组和排序的更多相关文章
- C# WPF DataGrid 分组(Group)
原文:C# WPF DataGrid 分组(Group) 效果如图,每个列的名字可以自定义.我随便用了”File”和”Attachment Name”. 在Window的Resources里面设置S ...
- WPF DataGrid 服务端排序
转载:http://www.kecq.com/artic-10322303.html 以前做了一个数据客户端,不过是直接连数据库的,现在认为这种方式不太好,于是改成服务端RESTful API+客户端 ...
- WPF DataGrid 分组
public ListCollectionView collection; collection = new ListCollectionView(obj.empData); collection.G ...
- WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.
WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次 悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ...
- WPF DataGrid常用属性记录
WPF DataGrid常用属性记录 组件常用方法: BeginEdit:使DataGrid进入编辑状态. CancelEdit:取消DataGrid的编辑状态. CollapseRowGroup:闭 ...
- WPF DataGrid分页功能实现代码 修改原作者不能实现的部分
这两天需要给Datagrid加个分页,查找了一些相关的文章,发现有一个写了一个控件比较好,地址是 http://blog.csdn.net/zdw_wym/article/details/822189 ...
- WPF DataGrid自定义样式
微软的WPF DataGrid中有很多的属性和样式,你可以调整,以寻找合适的(如果你是一名设计师).下面,找到我的小抄造型的网格.它不是100%全面,但它可以让你走得很远,有一些非常有用的技巧和陷阱. ...
- WPF DataGrid 控件的运用
WPF DataGrid 控件的运用 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-23 参考: King Cobra 博客 ...
- WPF DATAGRID - COMMITTING CHANGES CELL-BY-CELL
In my recent codeproject article on the DataGrid I described a number of techniques for handling the ...
随机推荐
- 对团队项目的NABCD的分析
需求(N):我们的软件是面向广大想记录自己所爱动植物成长点滴的人.目前没有很好地软件,只有手机或者电脑上的笔记本和备忘录. 做法(A):我们的软件可以交流可以节约积累知识的时间,将记录从记事本中摘出来 ...
- having 的用法
聚合函数 where 后面不能直接使用聚合函数 处理函数 题目 编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱. 示例: +----+---------+ | Id | Email ...
- Hbase常用操作(增删改查)
Hbase常用操作(增删改查) [日期:2014-01-03] 来源:Linux社区 作者:net19880504 [字体:大 中 小] 运行Eclipse,创建一个新的Java工程“HBa ...
- lambda表达式和表达式树(深入理解c#)
1.Lambda形式 1). Lambda表达式最冗长的形式: (显式类型的参数列表)=>{语句} 2). 大多数时候,都可以用一个表达式来表示主体,该表达式的值是Lambda的结果,在这些情况 ...
- 再读c++primer plus 003
1.如果函数返回一个结构而不是一个指向结构的引用,将把整个结构复制到一个临时位置,再将这个拷贝复制给dup.但在返回值为引用时,将直接复制给变量,其效率更高. 2.返回引用时最重要的一点是,应避免返回 ...
- 基础知识之nginx重写规则
nginx重写规则 nginx rewrite 正则表达式匹配 大小写匹配 ~ 为区分大小写匹配 ~* 为不区分大小写匹配 !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 文件及目录匹配 -f ...
- 2019.02.09 bzoj1042: [HAOI2008]硬币购物(完全背包+容斥原理)
传送门 题意简述:有四种面值的硬币,现在qqq次询问(q≤1000)(q\le1000)(q≤1000),每次给出四种硬币的使用上限问最后刚好凑出sss块钱的方案数(s≤100000)(s\le100 ...
- 2019.01.20 bzoj2238: Mst(kruskal+树链剖分)
传送门 树链剖分菜题. 题意简述:给一个无向图,边有边权,每次询问删一条边(对后面的询问无影响)之后的最小生成树. 思路: 先跑一次kruskalkruskalkruskal并把跑出来的最小生成树给链 ...
- Windows上使用Git管理文件
今天在搜索ffmpeg相关资料时,需要通过.sh脚本文件下载git上的代码文件,最后通过在Windows上安装了git,并在git.bash中执行bash ffmpeg.sh解决了代码下载问题,顺便学 ...
- Go语言高级特性总结——Struct、Map与JSON之间的转化
Struct与Map之间互相转换 // Struct2Map convert struct to map func Struct2Map(st interface{}) map[string]inte ...