使用CollectionViewSource,通过Xaml绑定自动排序

这个是一个完成分组和排序功能的Datagrid,同样只是简单的绑定到List集合,后台不用额外的代码,所有功能都在Xaml中完成:
首先在UI中定义CollectionViewSource资源,在这里定义排序和分组的规则
WPF中定义如下:
- <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>
<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中定义如下:
- <UserControl.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>
- </UserControl.Resources>
- ...
- ...
- <sdk:DataGrid DataContext="{StaticResource ProductsGroup}" AutoGenerateColumns="False"
- ItemsSource="{Binding}" SelectedItem="{Binding SelectedProduct}" Width="300" >
- <sdk:DataGrid.Columns>
- <sdk:DataGridTextColumn Binding="{Binding ID}" Header="编号" />
- <sdk:DataGridTextColumn Binding="{Binding Name}" Header="名称" />
- <sdk:DataGridTextColumn Binding="{Binding Desc}" Header="说明" />
- </sdk:DataGrid.Columns>
- </sdk:DataGrid>
使用CollectionViewSource,通过Xaml绑定自动排序的更多相关文章
- 在WPF中使用变通方法实现枚举类型的XAML绑定
问题缘起 WPF的分层结构为编程带来了极大便利,XAML绑定是其最主要的特征.在使用绑定的过程中,大家都普遍的发现枚举成员的绑定是个问题.一般来说,枚举绑定多出现于与ComboBox配合的情况,此时我 ...
- 重新想象 Windows 8 Store Apps (53) - 绑定: 与 ObservableCollection CollectionViewSource VirtualizedFilesVector VirtualizedItemsVector 绑定
[源码下载] 重新想象 Windows 8 Store Apps (53) - 绑定: 与 ObservableCollection CollectionViewSource VirtualizedF ...
- DataGridView点击排序完成后如何禁止自动排序
Summary: Disable sorting after clicking DataGridView columnheader,Prevent databound DataGridView fro ...
- 禁用datagridview中的自动排序功能
把datagridview中的自动排序功能禁用自己收集的两种方法,看看吧①DataGridView中的Columns属性里面可以设置.进入"EditColumns"窗口后,在相应的 ...
- sublime插件 cssComb实现css自动排序及格式化
cssComb是一个实现css代码自动排序,当然顺便也实现了代码的格式化 安装: 首先需要打开sublime搜索安装csscomb插件(前提是已经安装了sublime的package control) ...
- yii去掉自动排序功能
Yii去掉自动排序功能并自定义排序 public function search($params) { $query = SvnManage::find()->addOrderBy([ 'cre ...
- C++的STL之map自动排序特性
#include <iostream> #include <map> using namespace std; int main() {方法一: map<int,int& ...
- DataGridview的自动排序设置
如图,自动排序是每一列的属性,而不是整个datagridview的属性,之前一直在datagridview的属性中找不到,原来是在列的属性中
- JS实现点击表头表格自动排序(含数字、字符串、日期)
这篇文章主要介绍了利用JS如何实现点击表头后表格自动排序,其中包含数字排序.字符串排序以及日期格式的排序,文中给出了完整的示例代码,并做了注释,相信大家都能看懂,感兴趣的朋友们一起来看看吧. < ...
随机推荐
- 【熊掌号mip插件】织梦DEDECMS百度熊掌号mip改造教程
第一部分:模板修改 1.js部分:删除或使用现有组件替换 2.调用百度mip文件: head里加<link rel="stylesheet" type="text/ ...
- react-native基础教程(1)
转载链接:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/react-native-foundation-course/ React ...
- 简单理解Linux的Loopback接口
Linu支持环回接口( Loopback Interface),以允许运行在同一台主机上的客户程序和服务器程序通TCP/IP进行通信. A 类网络127就是为环回接口预留的 .根据惯例,大多数系统把I ...
- Delphi发布ActiveX控件 制作CAB包 数字签名相关
文件: SignTool.rar 大小: 84KB 下载: 下载 最近我正在研究ActiveX技术.我使用Delphi 7创建了一个具有ActiveForm的ActiveX控件应用程序.这个控件产生一 ...
- 一篇文章让你学透Linux系统中的more命令
Linux 下有很多实用工具可以让你在终端界面查看文本文件.其中一个就是 more. more 跟我之前另一篇文章里写到的工具 —— less 很相似.它们之间的主要不同点在于 more 只允许你向前 ...
- Spark2.3(三十六):根据appName验证某个app是否在运行
具体脚本 #/bin/sh #LANG=zh_CN.utf8 #export LANG export SPARK_KAFKA_VERSION=0.10 export LANG=zh_CN.UTF- # ...
- .NET 用 Unity 依赖注入——概述注册和解析类型(1)
本文内容 Unity 概述 环境 一个真实的例子 类型注册(Type Registrations) 解析类型(Resolving Types) 跳槽,新公司使用了 Unity,初步看了一下,公司的使用 ...
- 未能找到temp\select2.cur的一部分
环境 操作系统:win10 家庭普通版本 x64 账户类型:管理员 SuperMap:9D 打开自定义的应用程序时,会报错:未能找到路径"C:\Users\user\AppData\Loca ...
- Oracle 12c: RMAN restore/recover pluggable database
查看数据库状态 运行在归档模式,可拔插数据库name=pdborcl SQL> archive log list; Database log mode Archive Mode Automati ...
- logstash 学习小记
logstash 学习小记 标签(空格分隔): 日志收集 Introduce Logstash is a tool for managing events and logs. You can use ...