WPF 精修篇 DataGrid 数据源排序
效果

-
<DataGrid x:Name="datagrid" ItemsSource="{Binding ElementName=Mwindow, Path=Preson}" Margin="0,0,0,20">
-
<DataGrid.Columns>
-
<DataGridTextColumn Binding="{Binding Name}" Header="Name"></DataGridTextColumn>
-
<DataGridTextColumn Binding="{Binding Address}" Header="Address"></DataGridTextColumn>
-
<DataGridTextColumn Binding="{Binding Age}" Header="Age"></DataGridTextColumn>
-
</DataGrid.Columns>
-
</DataGrid>
-
<CheckBox x:Name="sort" Content="排序" HorizontalAlignment="Left" Margin="466,300,0,0" VerticalAlignment="Top" Checked="CheckBox_Checked"/>
-
public partial class MainWindow : Window
-
{
-
public MainWindow()
-
{
-
InitializeComponent();
-
-
Preson = new ObservableCollection<Preson>() {
-
new Preson() { Name = "慧哥1", Address = "大连1", Age = 31 },
-
new Preson() { Name = "慧哥2", Address = "大连2", Age = 32 },
-
new Preson() { Name = "慧哥3", Address = "大连3", Age = 33 },
-
new Preson() { Name = "慧哥4", Address = "大连4", Age = 34 },
-
new Preson() { Name = "慧哥5", Address = "大连5", Age = 35 },
-
new Preson() { Name = "123", Address = "大连6", Age = 12 },
-
new Preson() { Name = "444", Address = "大连7", Age = 14 },
-
new Preson() { Name = "222", Address = "大连8", Age = 33 },
-
new Preson() { Name = "1312", Address = "大连9", Age = 22 }
-
};
-
-
-
-
}
-
-
-
-
public ObservableCollection<Preson> Preson
-
{
-
get { return (ObservableCollection<Preson>)GetValue(PresonProperty); }
-
set { SetValue(PresonProperty, value); }
-
}
-
-
// Using a DependencyProperty as the backing store for Preson. This enables animation, styling, binding, etc...
-
public static readonly DependencyProperty PresonProperty =
-
DependencyProperty.Register("Preson", typeof(ObservableCollection<Preson>), typeof(MainWindow), new PropertyMetadata(null));
-
-
private void CheckBox_Checked(object sender, RoutedEventArgs e)
-
{
-
var cvs = CollectionViewSource.GetDefaultView(datagrid.ItemsSource);
-
if(cvs!=null&&cvs.CanSort)
-
{
-
cvs.SortDescriptions.Clear();
-
if (sort.IsChecked == true)
-
{
-
cvs.SortDescriptions.Add(new System.ComponentModel.SortDescription("Age", System.ComponentModel.ListSortDirection.Descending));
-
}
-
-
}
-
}
-
-
-
-
-
-
-
}
留作记录
WPF 精修篇 DataGrid 数据源排序的更多相关文章
- WPF 精修篇 DataGrid 筛选
原文:WPF 精修篇 DataGrid 筛选 DataGrid也可以分组 但是用的地方不多 就没写 筛选还是可以的 比如Datagrid数据量比较大 要做数据筛选 贴码 <DataGrid x: ...
- WPF 精修篇 数据触发器
原文:WPF 精修篇 数据触发器 数据触发器 可以使用Binding 来绑定控件 或者数据源 来触发相关动作 举栗子 <Window.Resources> <Style Target ...
- WPF 精修篇 属性触发器
原文:WPF 精修篇 属性触发器 属性触发器是通过 某个条件触发改变属性 通过无代码实现功能 <Style TargetType="{x:Type Label}"> ...
- WPF 精修篇 样式继承
原文:WPF 精修篇 样式继承 这个 是新知识 样式可以继承 <Style x:Key="TextBlockStyleBase" TargetType="{x:Ty ...
- WPF 精修篇 自定义控件
原文:WPF 精修篇 自定义控件 自定义控件 因为没有办法对界面可视化编辑 所以用来很少 现在实现的是 自定义控件的 自定义属性 和自定义方法 用VS 创建自定义控件后 会自动创建 Themes 文件 ...
- WPF 精修篇 用户控件
原文:WPF 精修篇 用户控件 增加用户控件 数据绑定还是用依赖属性 使用的事件 就委托注册一下 public delegate void ButtonClick(object b,EventArgs ...
- WPF 精修篇 数据绑定到对象
原文:WPF 精修篇 数据绑定到对象 数据绑定到对象 首先 我们需要一个对象 public class Preson { private string name; public string Name ...
- WPF 精修篇 数据绑定 更新通知
原文:WPF 精修篇 数据绑定 更新通知 开始更新一点有意思的了 首先 数据绑定 其中之一 Element 绑定 看例子 <Window x:Class="WpfApplicatio ...
- WPF 精修篇 依赖属性
原文:WPF 精修篇 依赖属性 依赖属性使用场景 1. 希望可在样式中设置属性. 2. 希望属性支持数据绑定. 3. 希望可使用动态资源引用设置属性. 4. 希望从元素树中的父元素自动继承属性值. 5 ...
随机推荐
- Java_JDBC 连接
今天,接着上一篇( mysql 数据库 )的基础上,我就写一下 Java 怎样连接数据库,并且操作数据库. 首先,我们先来准备一下数据库连接的驱动: mysql 的 jar 包下载地址:https:/ ...
- linux下的服务器上传与下载
上传 scp 文件 用户名@服务器ip 服务器保存路径 例如:scp bookmarks_2019_6_24.html root@192.168.0.103:/home 下载 scp 用户名@服务器i ...
- Redis 到底是怎么实现“附近的人”这个功能的呢?
作者简介 万汨,饿了么资深开发工程师.iOS,Go,Java均有涉猎.目前主攻大数据开发.喜欢骑行.爬山. 前言:针对“附近的人”这一位置服务领域的应用场景,常见的可使用PG.MySQL和MongoD ...
- SQL实用技巧:如何判断一个值是否为数字的方法
检测是不是数字型的数据, 两种方法 1. ISNUMERIC ( expression ) 2. PATINDEX ( ‘%pattern%‘ , expression ) 1. ISNUMERIC ...
- A Code Farmer‘s Entertainment
My guitar playing and singing 码农的自娱自乐 https://v.youku.com/v_show/id_XNDM4NTY1MTEwNA==.html?spm=a2hzp ...
- COUNT(*)、COUNT(主键)、COUNT(1)
MyISAM引擎,记录数是结构的一部分,已存cache在内存中; InnoDB引擎,需要重新计算,id是主键的话,会加快扫描速度: 所以select count(*) MyISAM完胜! MyISA ...
- Creating a Physical Standby from Primary on Version 12c (Doc ID 1570958.1)
Creating a Physical Standby from Primary on Version 12c (Doc ID 1570958.1) APPLIES TO: Oracle Databa ...
- 使用Active Database Duplication创建跨平台Data Guard设置 (Windows/Linux) (Doc ID 881421.1)
Using Active Database Duplication to Create Cross Platform Data Guard Setup (Windows/Linux) (Doc ID ...
- java之子类对象实例化过程
假设现在有这么一个父类: public class Person{ public Person(){} public String name = "tom"; public int ...
- 2019年全国高校计算机能力挑战赛初赛C++语言解答
1; 题目一 16.电商促销某种商品时,希望通过打折鼓励消费者组团消费.已知,团队消费金额=该团的人数和*商品单价.打折规则如下:当组团消费者数量不满50人时,商品消费金额没有折扣:消费者数量大于等于 ...