这个是一个完成分组和排序功能的Datagrid,同样只是简单的绑定到List集合,后台不用额外的代码,所有功能都在Xaml中完成:

首先在UI中定义CollectionViewSource资源,在这里定义排序和分组的规则

WPF中定义如下:

  1. <Window.Resources>
  2. <CollectionViewSource x:Key="ProductsGroup" Source="{Binding Products}">
  3. <- 分组描述->
  4. <CollectionViewSource.GroupDescriptions>
  5. <PropertyGroupDescription PropertyName="ProductDate" />
  6. </CollectionViewSource.GroupDescriptions>
  7. <--排序描述-->
  8. <CollectionViewSource.SortDescriptions>
  9. <scm:SortDescription PropertyName="ProductDate" Direction="Descending" />
  10. <scm:SortDescription PropertyName="ID" Direction="Ascending" />
  11. </CollectionViewSource.SortDescriptions>
  12. </CollectionViewSource>
  13. </Window.Resources>
  14. ...
  15. ...
  16. <DataGrid DataContext="{StaticResource ProductsGroup}" AutoGenerateColumns="False"
  17. ItemsSource="{Binding}" SelectedItem="{Binding SelectedProduct}" CanUserAddRows="False">
  18. <DataGrid.GroupStyle>
  19. <GroupStyle>
  20. <GroupStyle.HeaderTemplate>
  21. <DataTemplate>
  22. <TextBlock  x:Name="txt"  Background="LightBlue" FontWeight="Bold"
  23. Foreground="White" Margin="1" Padding="4,2,0,2"
  24. Text="{Binding Name,StringFormat='生产日期:/{0/}'}" />
  25. </DataTemplate>
  26. </GroupStyle.HeaderTemplate>
  27. </GroupStyle>
  28. </DataGrid.GroupStyle>
  29. <DataGrid.Columns>
  30. <DataGridTextColumn Binding="{Binding ID}" Header="编号" />
  31. <DataGridTextColumn Binding="{Binding Name}" Header="名称" />
  32. <DataGridTextColumn Binding="{Binding Desc}" Header="说明" />
  33. </DataGrid.Columns>
  34. </DataGrid>

<Window.Resources>
<CollectionViewSource x:Key="ProductsGroup" Source="{Binding Products}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="ProductDate" />
</CollectionViewSource.GroupDescriptions>
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="ProductDate" Direction="Descending" />
<scm:SortDescription PropertyName="ID" Direction="Ascending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>

...
...

<DataGrid DataContext="{StaticResource ProductsGroup}" AutoGenerateColumns="False"
ItemsSource="{Binding}" SelectedItem="{Binding SelectedProduct}" CanUserAddRows="False">
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock x:Name="txt" Background="LightBlue" FontWeight="Bold"
Foreground="White" Margin="1" Padding="4,2,0,2"
Text="{Binding Name,StringFormat='生产日期:/{0/}'}" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ID}" Header="编号" />
<DataGridTextColumn Binding="{Binding Name}" Header="名称" />
<DataGridTextColumn Binding="{Binding Desc}" Header="说明" />
</DataGrid.Columns>
</DataGrid>

SL中定义如下:

  1. <UserControl.Resources>
  2. <CollectionViewSource x:Key="ProductsGroup" Source="{Binding Products}">
  3. <CollectionViewSource.GroupDescriptions>
  4. <PropertyGroupDescription PropertyName="ProductDate" />
  5. </CollectionViewSource.GroupDescriptions>
  6. <CollectionViewSource.SortDescriptions>
  7. <scm:SortDescription PropertyName="ProductDate" Direction="Descending" />
  8. <scm:SortDescription PropertyName="ID" Direction="Ascending" />
  9. </CollectionViewSource.SortDescriptions>
  10. </CollectionViewSource>
  11. </UserControl.Resources>
  12. ...
  13. ...
  14. <sdk:DataGrid DataContext="{StaticResource ProductsGroup}" AutoGenerateColumns="False"
  15. ItemsSource="{Binding}" SelectedItem="{Binding SelectedProduct}" Width="300" >
  16. <sdk:DataGrid.Columns>
  17. <sdk:DataGridTextColumn Binding="{Binding ID}" Header="编号" />
  18. <sdk:DataGridTextColumn Binding="{Binding Name}" Header="名称" />
  19. <sdk:DataGridTextColumn Binding="{Binding Desc}" Header="说明" />
  20. </sdk:DataGrid.Columns>
  21. </sdk:DataGrid>

使用CollectionViewSource,通过Xaml绑定自动排序的更多相关文章

  1. 在WPF中使用变通方法实现枚举类型的XAML绑定

    问题缘起 WPF的分层结构为编程带来了极大便利,XAML绑定是其最主要的特征.在使用绑定的过程中,大家都普遍的发现枚举成员的绑定是个问题.一般来说,枚举绑定多出现于与ComboBox配合的情况,此时我 ...

  2. 重新想象 Windows 8 Store Apps (53) - 绑定: 与 ObservableCollection CollectionViewSource VirtualizedFilesVector VirtualizedItemsVector 绑定

    [源码下载] 重新想象 Windows 8 Store Apps (53) - 绑定: 与 ObservableCollection CollectionViewSource VirtualizedF ...

  3. DataGridView点击排序完成后如何禁止自动排序

    Summary: Disable sorting after clicking DataGridView columnheader,Prevent databound DataGridView fro ...

  4. 禁用datagridview中的自动排序功能

    把datagridview中的自动排序功能禁用自己收集的两种方法,看看吧①DataGridView中的Columns属性里面可以设置.进入"EditColumns"窗口后,在相应的 ...

  5. sublime插件 cssComb实现css自动排序及格式化

    cssComb是一个实现css代码自动排序,当然顺便也实现了代码的格式化 安装: 首先需要打开sublime搜索安装csscomb插件(前提是已经安装了sublime的package control) ...

  6. yii去掉自动排序功能

    Yii去掉自动排序功能并自定义排序 public function search($params) { $query = SvnManage::find()->addOrderBy([ 'cre ...

  7. C++的STL之map自动排序特性

    #include <iostream> #include <map> using namespace std; int main() {方法一: map<int,int& ...

  8. DataGridview的自动排序设置

    如图,自动排序是每一列的属性,而不是整个datagridview的属性,之前一直在datagridview的属性中找不到,原来是在列的属性中

  9. JS实现点击表头表格自动排序(含数字、字符串、日期)

    这篇文章主要介绍了利用JS如何实现点击表头后表格自动排序,其中包含数字排序.字符串排序以及日期格式的排序,文中给出了完整的示例代码,并做了注释,相信大家都能看懂,感兴趣的朋友们一起来看看吧. < ...

随机推荐

  1. reac-native环境搭建

    转载链接:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/react-native%E7%8E%AF%E5%A2%83%E6%90% ...

  2. Reactor 3 学习笔记(2)

    接上篇继续学习各种方法: 4.9.reduce/reduceWith @Test public void reduceTest() { Flux.range(1, 10).reduce((x, y) ...

  3. 使用 Docker 搭建 Tomcat 运行环境

    转自: http://m.oschina.net/blog/616526, 版权归原作者所有. 1 Docker与虚拟机     2 搭建过程 2.1 准备宿主系统 准备一个 CentOS 7操作系统 ...

  4. 解密gzip压缩的网页数据流(转)

    因为采集某个网页遇到问题,一直无法获取页面数据. 经过一番排查,发现该网站会检查客户端的Header信息,如果遇到不明确的Header信息就直接否定,返回0数据. 如果Header信息正确,就会返回经 ...

  5. 在 Visual Studio 2010 中配置SharpPcap

    最近需要在C#下写一个抓取ARP包的程序,网上找来找去,在C#下只能用SharpPcap来做了.SharpPcap是作者把winPcap用C#重新封装而来的,详细信息见如下的链接. SharpPcap ...

  6. MySQL架构与业务总结图

    MySQL架构与业务总结图如下:

  7. PHP中日志相关处理

    内置函数: 1.error_log() ,第三个参数不能是绝对路径,必须是相对路径.写入文件: error_log("warn:\nthis is a warn!\n",3,&qu ...

  8. How to measure IOPS for VMware

    http://blog.synology.com/blog/?p=2225 Executive SummaryThis article, intended towards IT Professiona ...

  9. <转>详解C++的模板中typename关键字的用法

    用处1, 用在模板定义里, 标明其后的模板参数是类型参数. 例如: template<typename T, typename Y> T foo(const T& t, const ...

  10. ceph 对象存储跨机房容灾

    场景分析 每个机房的Ceph都是独立的cluster,彼此之间没有任何关系. 多个机房都独立的提供对象存储功能,每个Ceph Radosgw都有自己独立的命名空间和存储空间. 这样带来两个问题: 针对 ...