最近在优化WPF性能时, 发现在特定条件下BindingList比ObservableCollection性能更高, 因为它提供Disable/Enable 更改通知的方法。这样我们可以不需要很频繁的通知UI去更新, 而是等所有操作都做完后再通知。

然而, 默认的BindingList不支持Sort, 需要我们实现。
 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的更多相关文章

  1. 在 ASP.NET MVC 中充分利用 WebGrid (microsoft 官方示例)

    在 ASP.NET MVC 中充分利用 WebGrid https://msdn.microsoft.com/zh-cn/magazine/hh288075.aspx Stuart Leeks 下载代 ...

  2. Hibernate的映射文件

    映射文件的结构和属性 一个映射文件(mapping file)由一个根节点<hibernate-mapping>和多个<class>节点组成, 首先看看根节点<hiber ...

  3. iOS -类目,延展,协议

    1.类目 类目就是为已存在的类添加新的方法.但是不能添加实例变量.比如系统的类,我们看不到他的.m文件,所以没有办法用直接添加方法的方式去实现. @interface NSMutableArray ( ...

  4. MongoDb 聚合报错

    聚合框架它是数据聚合的一个新框架,其概念类似于数据处理的管道. 每个文档通过一个由多个节点组成的管道,每个节点有自己特殊的功能(分组.过滤等),文档经过管道处理后,最后输出相应的结果. 管道基本的功能 ...

  5. javascript错误处理与调试(转)

    JavaScript 在错误处理调试上一直是它的软肋,如果脚本出错,给出的提示经常也让人摸不着头脑. ECMAScript 第 3 版为了解决这个问题引入了 try...catch 和 throw 语 ...

  6. Impala 源码分析-FE

    By yhluo 2015年7月29日 Impala 3 Comments Impala 源代码目录结构 SQL 解析 Impala 的 SQL 解析与执行计划生成部分是由 impala-fronte ...

  7. [WPF]ListView点击列头排序功能实现

    [转]   [WPF]ListView点击列头排序功能实现 这是一个非常常见的功能,要求也很简单,在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了. ...

  8. 【Hibernate】set排序

    使用hibernate进行一对多操作的时候,普遍使用HashSet进行操作.但是HashSet是无序集合,对此可以使用TreeSet进行排序. 1.将HashSet改为TreeSet private ...

  9. 第一百二十三节,JavaScript错误处理与调试

    JavaScript错误处理与调试 学习要点: 1.浏览器错误报告 2.错误处理 3.错误事件 4.错误处理策略 5.调试技术 6.调试工具 JavaScript在错误处理调试上一直是它的软肋,如果脚 ...

随机推荐

  1. DWR3.0(Direct Web Remoting)实践

    “DWR is a Java library that enables Java on the server and JavaScript in a browser to interact and c ...

  2. request 获取请求头

    /********************************************************servlet页面********************************** ...

  3. HDFS源码分析EditLog之读取操作符

    在<HDFS源码分析EditLog之获取编辑日志输入流>一文中,我们详细了解了如何获取编辑日志输入流EditLogInputStream.在我们得到编辑日志输入流后,是不是就该从输入流中获 ...

  4. Linux下实现RAID

    一.实验目的 1.掌握Linux系统下软RAID的实现方法: 2.掌握RAID5的配置过程: 3. 通过实验熟悉RAID.5的特点. 二.实验内容及步骤 1.在VMware中创建一台Linux. 2. ...

  5. 给定一颗完全二叉树,给每一层添加上next的指针,从左边指向右边

    给你机会发出声音,但是不给你机会证明高层的决定是错的 RT: 时间复杂度O(n) 空间复杂度O(1)  原理就是有指针指向父节点和当前的节点,左孩子必指向右孩子,右孩子必指向父节点的下一个节点的左孩子 ...

  6. grep命令:查看配置文件未注释行(转)

    FROM: https://linux.cn/article-6958-1.html 可以使用 UNIX/BSD/OS X/Linux 这些操作系统自身提供的 grep,sed,awk,perl或者其 ...

  7. 关于TextView 的属性

    一.设置不同的字体和颜色值:questionDesTextView=(TextView)findViewById(R.id.question_des); SpannableStringBuilder ...

  8. SPOJ LCS2 - Longest Common Substring II 后缀自动机 多个串的LCS

    LCS2 - Longest Common Substring II no tags  A string is finite sequence of characters over a non-emp ...

  9. C#单元测试(转)

    C#,单元测试入门(以下内容可能来自网络) 一.什么叫单元测试(unit testing)? 是指对软件中的最小可测试单元进行检查和验证.对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体 ...

  10. Asynchronous_method_invocation 异步方法调用 让步 yielding

    zh.wikipedia.org/wiki/同步 [同步不同事件发生 时间一致] 同步(英语:Synchronization),指在一个系统中所发生的事件(event),之间进行协调,在时间上出现一致 ...