之前一直用的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分组和排序的更多相关文章

  1. C# WPF DataGrid 分组(Group)

    原文:C# WPF DataGrid 分组(Group) 效果如图,每个列的名字可以自定义.我随便用了”File”和”Attachment Name”.  在Window的Resources里面设置S ...

  2. WPF DataGrid 服务端排序

    转载:http://www.kecq.com/artic-10322303.html 以前做了一个数据客户端,不过是直接连数据库的,现在认为这种方式不太好,于是改成服务端RESTful API+客户端 ...

  3. WPF DataGrid 分组

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

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

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

  5. WPF DataGrid常用属性记录

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

  6. WPF DataGrid分页功能实现代码 修改原作者不能实现的部分

    这两天需要给Datagrid加个分页,查找了一些相关的文章,发现有一个写了一个控件比较好,地址是 http://blog.csdn.net/zdw_wym/article/details/822189 ...

  7. WPF DataGrid自定义样式

    微软的WPF DataGrid中有很多的属性和样式,你可以调整,以寻找合适的(如果你是一名设计师).下面,找到我的小抄造型的网格.它不是100%全面,但它可以让你走得很远,有一些非常有用的技巧和陷阱. ...

  8. WPF DataGrid 控件的运用

    WPF DataGrid 控件的运用 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-23 参考: King Cobra 博客 ...

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

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

随机推荐

  1. merge_关系

    create(olive:Person{chau:"Bill White",name:"Olive Stone",bornin:"New York&q ...

  2. easyui validate -- radio、checkbox 校验扩展,事件域名

    事件域名: $(dom).on('click.myNameSpace',function(){ ... }),其中‘.myNameSpace’便是域名: 目前作用:$(dom).off('click. ...

  3. PLSQL数组

    declare type t_varchar_arr is TABLE OF varchar2(60); type t_number_arr is TABLE OF number; v_date t_ ...

  4. IOS初级:SDWebImage

    简单用法 #import "ViewController.h" #import "SDWebImage/UIImageView+WebCache.h" @int ...

  5. 【搜索】Dungeon Master

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  6. kbmMW均衡负载与容灾(2)(转载红鱼儿)

    集中式均衡负载 为实现集中式均衡负载方案,需要实现两个不同的应用服务器,一个是只包含均衡负载组件再无其他内容的应用服务器,可称之为均衡负载应用服务器,下文简称LB Server,另外一个就是包含一个或 ...

  7. 2018.11.24 poj3415Common Substrings(后缀数组+单调栈)

    传送门 常数实在压不下来(蒟蒻开O(3)都过不了). 但有正确性233. 首先肯定得把两个字符串接在一起. 相当于heightheightheight数组被height<kheight<k ...

  8. fastjson 错误解决方案详情 com.alibaba.fastjson.JSONException: syntax error, expect {, actual EOF, pos 1410

    原因: 前端传递的数组过于复杂,倒是出现这种问题,前端采用vue axios,发送请求,后端java接收代码,实现前后端分离 后端就收fastjson接收json,进行业务处理,后端Controlle ...

  9. MySQL导入导出表数据

    原文链接:http://blog.163.com/yang_jianli/blog/static/1619900062010111011041228/ 1.这里的导出和mysqldump不同,只是导出 ...

  10. Java EE Servlet 几个path

    ContextPath Context ['kɒntekst] 不识庐山真面目,只缘身在此山中. 相对路径 RealPath 绝对路径 ServletPath 就是servlet-mapping 中 ...