使用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如何实现点击表头后表格自动排序,其中包含数字排序.字符串排序以及日期格式的排序,文中给出了完整的示例代码,并做了注释,相信大家都能看懂,感兴趣的朋友们一起来看看吧. < ...
随机推荐
- 未能加载文件或程序集“Benlai.SOA.Framework.Common, Version=1.4.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。系统找不到指定的文件。
未能加载文件或程序集“Benlai.SOA.Framework.Common, Version=1.4.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个 ...
- jdk9+版本的bug
今天从jvm大神"你假笨"的公众号上,看到一个jdk 9+版本的编译bug,记录一下: public class JavacEvalBug{ private static Stri ...
- Shutting down CodePlex 03/31/2017
Almost 11 years after we created CodePlex, it’s time to say goodbye. We launched CodePlex in 2006 b ...
- WordPress主题开发:输出指定页面导航
实例: <ul> <li class="widget widget_nav_menu"> <?php if(is_page(array(12,14,1 ...
- Scala:Functional Objects
先上代码 class FunctionalObjects(var _x: Int, var _y: Int) { require(_x > 0) require(_y > 0) def t ...
- SpringBoot日志logback-spring.xml分环境log4j logback slf4j区别 springboot日志设置
转载. https://blog.csdn.net/qianyiyiding/article/details/76565810 springboot按照profile进行打印日志log4j logba ...
- [Mockito] Spring Unit Testing with Mockito
It is recommened to write unit testing with Mockito in Spring framework, because it is much faster w ...
- 从Encoder到Decoder实现Seq2Seq模型
https://zhuanlan.zhihu.com/p/27608348 更新:感谢@Gang He指出的代码错误.get_batches函数中第15行与第19行,代码已经重新修改,GitHub已更 ...
- WPF获取当前用户控件的父级窗体
方式一.通过当前控件名获取父级窗体 Window targetWindow = Window.GetWindow(button); 方式二.通过当前控件获取父级窗体 Window parentWind ...
- Shell获取某目录下所有文件夹的名称
查看目录下面的所有文件: #!/bin/bash cd /目标目录 for file in $(ls *) do echo $file done 延伸的方法,查看目录下面的所有目录 #!/bin/ba ...