为BindingList添加Sort
最近在优化WPF性能时, 发现在特定条件下BindingList比ObservableCollection性能更高, 因为它提供Disable/Enable 更改通知的方法。这样我们可以不需要很频繁的通知UI去更新, 而是等所有操作都做完后再通知。
public class SortableBindingList<T> : BindingList<T>
{
// Fields
private bool isSorted;
private ListSortDirection listSortDirection;
private PropertyDescriptor propertyDescriptor; // Methods
public SortableBindingList()
{
} public SortableBindingList(IList<T> list)
: this()
{
base.Items.Clear();
foreach (T local in list)
{
base.Add(local);
}
} protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
{
(base.Items as List<T>).Sort(this.GetComparisionDelegate(prop, direction));
} private Comparison<T> GetComparisionDelegate(PropertyDescriptor propertyDescriptor, ListSortDirection direction)
{
return delegate(T t1, T t2)
{
int num2;
((SortableBindingList<T>)this).propertyDescriptor = propertyDescriptor;
((SortableBindingList<T>)this).listSortDirection = direction;
((SortableBindingList<T>)this).isSorted = true;
int num = (direction == ListSortDirection.Ascending) ? 1 : -1;
if (propertyDescriptor.PropertyType == typeof(string))
{
num2 = StringComparer.CurrentCulture.Compare(propertyDescriptor.GetValue(t1), propertyDescriptor.GetValue(t2));
}
else
{
num2 = Comparer.Default.Compare(propertyDescriptor.GetValue(t1), propertyDescriptor.GetValue(t2));
}
return (num * num2);
};
} protected override void RemoveSortCore()
{
this.isSorted = false;
this.propertyDescriptor = base.SortPropertyCore;
this.listSortDirection = base.SortDirectionCore;
} // Properties
protected override bool IsSortedCore
{
get
{
return this.isSorted;
}
} protected override ListSortDirection SortDirectionCore
{
get
{
return this.listSortDirection;
}
} protected override PropertyDescriptor SortPropertyCore
{
get
{
return this.propertyDescriptor;
}
} protected override bool SupportsSortingCore
{
get
{
return true;
}
}
}
调用RaiseListChangedEvents = false 或者RaiseListChangedEvents = true来控制更改通知,
调用ResetBindings()来全部更新, 或者ResetItem(index)来更新指定索引
为BindingList添加Sort的更多相关文章
- 在 ASP.NET MVC 中充分利用 WebGrid (microsoft 官方示例)
在 ASP.NET MVC 中充分利用 WebGrid https://msdn.microsoft.com/zh-cn/magazine/hh288075.aspx Stuart Leeks 下载代 ...
- Hibernate的映射文件
映射文件的结构和属性 一个映射文件(mapping file)由一个根节点<hibernate-mapping>和多个<class>节点组成, 首先看看根节点<hiber ...
- iOS -类目,延展,协议
1.类目 类目就是为已存在的类添加新的方法.但是不能添加实例变量.比如系统的类,我们看不到他的.m文件,所以没有办法用直接添加方法的方式去实现. @interface NSMutableArray ( ...
- MongoDb 聚合报错
聚合框架它是数据聚合的一个新框架,其概念类似于数据处理的管道. 每个文档通过一个由多个节点组成的管道,每个节点有自己特殊的功能(分组.过滤等),文档经过管道处理后,最后输出相应的结果. 管道基本的功能 ...
- javascript错误处理与调试(转)
JavaScript 在错误处理调试上一直是它的软肋,如果脚本出错,给出的提示经常也让人摸不着头脑. ECMAScript 第 3 版为了解决这个问题引入了 try...catch 和 throw 语 ...
- Impala 源码分析-FE
By yhluo 2015年7月29日 Impala 3 Comments Impala 源代码目录结构 SQL 解析 Impala 的 SQL 解析与执行计划生成部分是由 impala-fronte ...
- [WPF]ListView点击列头排序功能实现
[转] [WPF]ListView点击列头排序功能实现 这是一个非常常见的功能,要求也很简单,在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了. ...
- 【Hibernate】set排序
使用hibernate进行一对多操作的时候,普遍使用HashSet进行操作.但是HashSet是无序集合,对此可以使用TreeSet进行排序. 1.将HashSet改为TreeSet private ...
- 第一百二十三节,JavaScript错误处理与调试
JavaScript错误处理与调试 学习要点: 1.浏览器错误报告 2.错误处理 3.错误事件 4.错误处理策略 5.调试技术 6.调试工具 JavaScript在错误处理调试上一直是它的软肋,如果脚 ...
随机推荐
- Oracle更新时间字段
update field set BEGINDATE=to_date('2015-05-03 10:30:20','yyyy-mm-dd hh24:mi:ss') where NOO='01'
- Linux kernel manpages
https://www.linuxquestions.org/questions/linux-newbie-8/man-pages-for-kernel-functions-758389/ 在Linu ...
- 自己编译GCC(compile gcc from source)
有的时候,我不是第一次遇到这种时候,编译内核时报出编译器BUG.如果是ubuntu还好一点,默认软件仓库中就有好几个GCC,换一换总能找到一个好使的,实在不行还有个Tooltrain的ppa,但Deb ...
- Angualr 实现复选框全选功能
html <html lang="en"> <head> <meta charset="UTF-8"> <title& ...
- hadoop 相关工具访问端口(转)
原文:http://www.tuicool.com/articles/BB3eArJ hadoop系统部署时用到不少端口.有的是Web UI所使用的,有的是内部通信所使用的,有的是监控所使用的.实际系 ...
- js 检测客户端网速
<!doctype html> <html> <head> <meta http-equiv=Content-Type content="text/ ...
- 九度OJ 1014:排名 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8267 解决:2469 题目描述: 今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排序,没有考虑每题的分 ...
- 【题解】Greatest Common Increasing Subsequence
[题解]Greatest Common Increasing Subsequence vj 唉,把自己当做DP入门选手来总结这道题吧,我DP实在太差了 首先是设置状态的技巧,设置状态主要就是要补充不漏 ...
- Django一对多的创建
1.一对多的应用场景: 例如:创建用户信息时候,需要选择一个用户类型[普通用户][金牌用户][铂金用户]等. 2.方法: class Business(models.Model): buss=mode ...
- Elasticsearch5 及 head插件 安装说明
Elasticsearch5.X及 head插件 安装说明: 1.下载elasticsearch安装文件: a) 下载官方源码: https://artifacts.elastic.co/downlo ...