原文:WPF 精修篇 DataGrid 数据源排序

效果


  1. <DataGrid x:Name="datagrid" ItemsSource="{Binding ElementName=Mwindow, Path=Preson}" Margin="0,0,0,20">
  2. <DataGrid.Columns>
  3. <DataGridTextColumn Binding="{Binding Name}" Header="Name"></DataGridTextColumn>
  4. <DataGridTextColumn Binding="{Binding Address}" Header="Address"></DataGridTextColumn>
  5. <DataGridTextColumn Binding="{Binding Age}" Header="Age"></DataGridTextColumn>
  6. </DataGrid.Columns>
  7. </DataGrid>
  8. <CheckBox x:Name="sort" Content="排序" HorizontalAlignment="Left" Margin="466,300,0,0" VerticalAlignment="Top" Checked="CheckBox_Checked"/>

  1. public partial class MainWindow : Window
  2. {
  3. public MainWindow()
  4. {
  5. InitializeComponent();
  6. Preson = new ObservableCollection<Preson>() {
  7. new Preson() { Name = "慧哥1", Address = "大连1", Age = 31 },
  8. new Preson() { Name = "慧哥2", Address = "大连2", Age = 32 },
  9. new Preson() { Name = "慧哥3", Address = "大连3", Age = 33 },
  10. new Preson() { Name = "慧哥4", Address = "大连4", Age = 34 },
  11. new Preson() { Name = "慧哥5", Address = "大连5", Age = 35 },
  12. new Preson() { Name = "123", Address = "大连6", Age = 12 },
  13. new Preson() { Name = "444", Address = "大连7", Age = 14 },
  14. new Preson() { Name = "222", Address = "大连8", Age = 33 },
  15. new Preson() { Name = "1312", Address = "大连9", Age = 22 }
  16. };
  17. }
  18. public ObservableCollection<Preson> Preson
  19. {
  20. get { return (ObservableCollection<Preson>)GetValue(PresonProperty); }
  21. set { SetValue(PresonProperty, value); }
  22. }
  23. // Using a DependencyProperty as the backing store for Preson. This enables animation, styling, binding, etc...
  24. public static readonly DependencyProperty PresonProperty =
  25. DependencyProperty.Register("Preson", typeof(ObservableCollection<Preson>), typeof(MainWindow), new PropertyMetadata(null));
  26. private void CheckBox_Checked(object sender, RoutedEventArgs e)
  27. {
  28. var cvs = CollectionViewSource.GetDefaultView(datagrid.ItemsSource);
  29. if(cvs!=null&&cvs.CanSort)
  30. {
  31. cvs.SortDescriptions.Clear();
  32. if (sort.IsChecked == true)
  33. {
  34. cvs.SortDescriptions.Add(new System.ComponentModel.SortDescription("Age", System.ComponentModel.ListSortDirection.Descending));
  35. }
  36. }
  37. }
  38. }

留作记录

WPF 精修篇 DataGrid 数据源排序的更多相关文章

  1. WPF 精修篇 DataGrid 筛选

    原文:WPF 精修篇 DataGrid 筛选 DataGrid也可以分组 但是用的地方不多 就没写 筛选还是可以的 比如Datagrid数据量比较大 要做数据筛选 贴码 <DataGrid x: ...

  2. WPF 精修篇 数据触发器

    原文:WPF 精修篇 数据触发器 数据触发器 可以使用Binding 来绑定控件 或者数据源 来触发相关动作 举栗子 <Window.Resources> <Style Target ...

  3. WPF 精修篇 属性触发器

    原文:WPF 精修篇 属性触发器 属性触发器是通过  某个条件触发改变属性 通过无代码实现功能 <Style TargetType="{x:Type Label}"> ...

  4. WPF 精修篇 样式继承

    原文:WPF 精修篇 样式继承 这个 是新知识 样式可以继承 <Style x:Key="TextBlockStyleBase" TargetType="{x:Ty ...

  5. WPF 精修篇 自定义控件

    原文:WPF 精修篇 自定义控件 自定义控件 因为没有办法对界面可视化编辑 所以用来很少 现在实现的是 自定义控件的 自定义属性 和自定义方法 用VS 创建自定义控件后 会自动创建 Themes 文件 ...

  6. WPF 精修篇 用户控件

    原文:WPF 精修篇 用户控件 增加用户控件 数据绑定还是用依赖属性 使用的事件 就委托注册一下 public delegate void ButtonClick(object b,EventArgs ...

  7. WPF 精修篇 数据绑定到对象

    原文:WPF 精修篇 数据绑定到对象 数据绑定到对象 首先 我们需要一个对象 public class Preson { private string name; public string Name ...

  8. WPF 精修篇 数据绑定 更新通知

    原文:WPF 精修篇 数据绑定 更新通知 开始更新一点有意思的了 首先 数据绑定  其中之一 Element 绑定 看例子 <Window x:Class="WpfApplicatio ...

  9. WPF 精修篇 依赖属性

    原文:WPF 精修篇 依赖属性 依赖属性使用场景 1. 希望可在样式中设置属性. 2. 希望属性支持数据绑定. 3. 希望可使用动态资源引用设置属性. 4. 希望从元素树中的父元素自动继承属性值. 5 ...

随机推荐

  1. Delphi 调用C# 编写的DLL方法

    近来,因工作需要,必须解决Delphi写的主程序调用C#写的dll的问题.在网上一番搜索,又经过种种试验,最终证明有以下两种方法可行:    编写C#dll的方法都一样,首先在vs2005中创建一个“ ...

  2. redhat 6.5 更换yum源

    新安装了redhat6.5.安装后,登录系统,使用yum update 更新系统.提示: Loaded plugins: product-id, security, subscription-mana ...

  3. Ubuntu下配置IP地址

    17.10版本之前: Ubuntu的网卡配置文件跟CentOS的不一样,Ubuntu的网卡配置文件是/etc/network/interfaces.我们用vi /etc/network/interfa ...

  4. 关于web.xml配置的那些事儿

    参考文章:重新认识web.xml

  5. Java之Arrays类

    Arrays概述 java.util.Arrays 此类包含用来操作数组的各种方法,比如排序和搜索等.其所有方法均为静态方法,调用起来非常简单. 常用方法 public static String t ...

  6. docker jenkins安装

    https://hub.docker.com/r/jenkins/jenkins jenkins的docker官方镜像地址 https://jenkins.io/ jenkins官方网站 环境: 阿里 ...

  7. 一个适合于.NET Core的超轻量级工作流引擎:Workflow-Core

    一.关于Workflow-Core 近期工作上有一个工作流的开发需求,自己基于面向对象和职责链模式捣鼓了一套小框架,后来在github上发现一个轻量级的工作流引擎轮子:Workflow-Core,看完 ...

  8. js中获取当前url路径

    可以使用 window.location 获取当前页面url.以下是一些简单应用. <script> $(function(){ // 返回 web 主机的域名,如:http://127. ...

  9. IDEA 护眼色设置

    首先做一些简单的记录,护眼色 等等的设置很久以前机器上已经设置过了,今天偶尔要在其他机器上重新做一些设置反而忘记了很多步骤, 设置后的HTML页面如下图所示: 默认情况下,当只是设置General通用 ...

  10. Swoole Redis 连接池的实现

    概述 这是关于 Swoole 入门学习的第九篇文章:Swoole Redis 连接池的实现. 第八篇:Swoole MySQL 连接池的实现 第七篇:Swoole RPC 的实现 第六篇:Swoole ...