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 ...
随机推荐
- Delphi 调用C# 编写的DLL方法
近来,因工作需要,必须解决Delphi写的主程序调用C#写的dll的问题.在网上一番搜索,又经过种种试验,最终证明有以下两种方法可行: 编写C#dll的方法都一样,首先在vs2005中创建一个“ ...
- redhat 6.5 更换yum源
新安装了redhat6.5.安装后,登录系统,使用yum update 更新系统.提示: Loaded plugins: product-id, security, subscription-mana ...
- Ubuntu下配置IP地址
17.10版本之前: Ubuntu的网卡配置文件跟CentOS的不一样,Ubuntu的网卡配置文件是/etc/network/interfaces.我们用vi /etc/network/interfa ...
- 关于web.xml配置的那些事儿
参考文章:重新认识web.xml
- Java之Arrays类
Arrays概述 java.util.Arrays 此类包含用来操作数组的各种方法,比如排序和搜索等.其所有方法均为静态方法,调用起来非常简单. 常用方法 public static String t ...
- docker jenkins安装
https://hub.docker.com/r/jenkins/jenkins jenkins的docker官方镜像地址 https://jenkins.io/ jenkins官方网站 环境: 阿里 ...
- 一个适合于.NET Core的超轻量级工作流引擎:Workflow-Core
一.关于Workflow-Core 近期工作上有一个工作流的开发需求,自己基于面向对象和职责链模式捣鼓了一套小框架,后来在github上发现一个轻量级的工作流引擎轮子:Workflow-Core,看完 ...
- js中获取当前url路径
可以使用 window.location 获取当前页面url.以下是一些简单应用. <script> $(function(){ // 返回 web 主机的域名,如:http://127. ...
- IDEA 护眼色设置
首先做一些简单的记录,护眼色 等等的设置很久以前机器上已经设置过了,今天偶尔要在其他机器上重新做一些设置反而忘记了很多步骤, 设置后的HTML页面如下图所示: 默认情况下,当只是设置General通用 ...
- Swoole Redis 连接池的实现
概述 这是关于 Swoole 入门学习的第九篇文章:Swoole Redis 连接池的实现. 第八篇:Swoole MySQL 连接池的实现 第七篇:Swoole RPC 的实现 第六篇:Swoole ...