WPF中DataGrid控件的过滤(Filter)性能分析及优化
DataGrid控件是一个列表控件, 可以进行过滤,排序等。本文主要针对DataGrid的过滤功能进行分析, 并提供优化方案。
public class CustomCollectionView<T> : ListCollectionView
{
private readonly DispatcherTimer _timerRefreshCalculate = new DispatcherTimer();
private readonly DispatcherTimer _timerRefreshUI = new DispatcherTimer();
private readonly DispatcherTimer _timerRefreshCurrentItem = new DispatcherTimer();
private bool _isRefreshingCalculate = false;
private object _oldSelectedItem = null;
public CustomCollectionView(IList list)
: base(list)
{
_timerRefreshUI.Interval = new TimeSpan(0, 0, 0, 0, 300);
_timerRefreshCurrentItem.Interval = new TimeSpan(0, 0, 0, 0, 500);
_timerRefreshCalculate.Interval = new TimeSpan(0, 0, 0, 0, 200);
}
#region Override Method
protected override void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (_isRefreshingCalculate)
{
return;
}
base.OnPropertyChanged(e);
}
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
{
if (_isRefreshingCalculate)
{
return;
}
base.OnCollectionChanged(args);
}
protected override void OnCurrentChanged()
{
if (_isRefreshingCalculate)
{
return;
}
base.OnCurrentChanged();
}
protected override void RefreshOverride()
{
CancelAllRefreshRequest();
StartRefresh();
}
#endregion
#region Public Method
public void CancelAllRefreshRequest()
{
_timerRefreshCurrentItem.Stop();
_timerRefreshCurrentItem.Tick -= TimerCurrentItem;
_timerRefreshUI.Stop();
_timerRefreshUI.Tick -= TimerUI;
_timerRefreshCalculate.Stop();
_timerRefreshCalculate.Tick -= TimerCalculate;
if (null != this.CurrentItem)
{
_oldSelectedItem = this.CurrentItem;
}
SetCurrent(null, -1);
}
#endregion
#region Private Method
private void StartRefresh()
{
_timerRefreshCurrentItem.Stop();
_timerRefreshCurrentItem.Tick -= TimerCurrentItem;
_timerRefreshUI.Stop();
_timerRefreshUI.Tick -= TimerUI;
_timerRefreshCalculate.Stop();
_timerRefreshCalculate.Tick -= TimerCalculate;
//begin to refresh from calculate, so set flag by true.
//and it shielded any collection action during the calculating time.
//this logic will avoid items are not correct at UI during refresh.
_isRefreshingCalculate = true;
_timerRefreshCalculate.Tick += TimerCalculate;
_timerRefreshCalculate.Start();
}
private void RefreshCalculate(CancellationToken? token)
{
_timerRefreshCalculate.Tick -= TimerCalculate;
if (null != token && null != token.Value)
{
token.Value.ThrowIfCancellationRequested();
}
_isRefreshingCalculate = true;
base.RefreshOverride();
_isRefreshingCalculate = false;
if (null != token && null != token.Value)
{
token.Value.ThrowIfCancellationRequested();
}
}
private void RefreshUI()
{
try
{
//detach timer
_timerRefreshUI.Tick -= TimerUI;
base.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
//set timer to refresh current item
_timerRefreshCurrentItem.Tick -= TimerCurrentItem;
_timerRefreshCurrentItem.Tick += TimerCurrentItem;
_timerRefreshCurrentItem.Start();
}
catch (OperationCanceledException)
{
return;
}
}
private void RefreshCurrentItem()
{
_timerRefreshCurrentItem.Tick -= TimerCurrentItem;
if (null == this.InternalList || this.InternalList.Count <= 0)
{
return;
}
if (null != _oldSelectedItem)
{
var index = this.InternalList.IndexOf(_oldSelectedItem);
if (index != -1)
{
SetCurrent(_oldSelectedItem, index);
}
else
{
SetCurrent(this.InternalList[0], 0);
}
}
else
{
SetCurrent(this.InternalList[0], 0);
}
//Set event to update UI
base.OnCurrentChanged();
this.OnPropertyChanged("IsCurrentAfterLast");
this.OnPropertyChanged("IsCurrentBeforeFirst");
this.OnPropertyChanged("CurrentPosition");
this.OnPropertyChanged("CurrentItem");
}
private void TimerCalculate(object sender, EventArgs e)
{
_timerRefreshCurrentItem.Stop();
_timerRefreshCurrentItem.Tick -= TimerCurrentItem;
_timerRefreshUI.Stop();
_timerRefreshUI.Tick -= TimerUI;
try
{
RefreshCalculate(null);
}
catch (OperationCanceledException)
{
}
_timerRefreshUI.Interval = new TimeSpan(0, 0, 0, 0, 50 + Math.Min((int)(this.InternalCount / 80), 300));
_timerRefreshUI.Tick += TimerUI;
_timerRefreshUI.Start();
}
private void TimerUI(object sender, EventArgs e)
{
RefreshUI();
}
private void TimerCurrentItem(object sender, EventArgs e)
{
RefreshCurrentItem();
}
private void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
#endregion
}
public class CustomCollection<T> : ObservableCollection<T>, ICollectionViewFactory
{
public CustomCollection()
{
}
public CustomCollection(List<T> list)
: base(list)
{
}
public CustomCollection(IEnumerable<T> collection)
: base(collection)
{
}
public ICollectionView CreateView()
{
return new CustomCollectionView<T>(this);
}
}
WPF中DataGrid控件的过滤(Filter)性能分析及优化的更多相关文章
- Working Experience - WPF 中 DataGrid 控件的应用
问题: 添加控件后, 编辑单元格会出现异常 绑定 ItemsSource 属性后, 更新绑定对象的数据, UI 不刷新 如何显示控件中 ComboBox 类型 解决方法: 绑定 ItemsSource ...
- WPF中DataGrid控件内Button的Command和CommandParameter的绑定
场景:视频上传功能,上传列表使用DataGrid控件,视频有不同的状态对应不同的操作,DataGrid中最后一列为操作列,里面是Button控件.希望点击Button后执行对应的操作,但是设置Butt ...
- WPF中Datagrid控件添加行号
/// <summary> /// 导入Excel文件按钮 /// </summary> /// <param name="sender">&l ...
- wpf 中DataGrid 控件的样式设置及使用
本次要实现的效果为: 这个DataGrid需要绑定一个集合对象,所以要先定义一个Experience类,包含三个字段 /// <summary> /// 定义工作经历类 /// </ ...
- WPF中查找控件的扩展类
在wpf中查找控件要用到VisualTreeHelper类,但这个类并没有按照名字查找控件的方法,于是搜索网络,整理出下面这个类,感觉用起来很是方便. 贴出来,供大家参考. /// <summa ...
- WPF的DataGrid控件从excel里复制数据然后粘贴
WPF的DataGrid控件不能像winform的DataGridView控件一样,支持值的粘贴.WPF的DataGrid控件本质上是跟数据绑定联系在一起,所以需要进行复制粘贴的操作,可以在wpf里用 ...
- WPF 4 DataGrid 控件(进阶篇一)
原文:WPF 4 DataGrid 控件(进阶篇一) 上一篇<WPF 4 DataGrid 控件(自定义样式篇)>中,我们掌握了DataGrid 列表头.行表头.行.单元格相关的 ...
- WPF 4 DataGrid 控件(进阶篇二)
原文:WPF 4 DataGrid 控件(进阶篇二) 上一篇<WPF 4 DataGrid 控件(进阶篇一)>中我们通过DataGridTemplateColumn 类自定义编辑 ...
- WPF 4 DataGrid 控件(基本功能篇)
原文:WPF 4 DataGrid 控件(基本功能篇) 提到DataGrid 不管是网页还是应用程序开发都会频繁使用.通过它我们可以灵活的在行与列间显示各种数据.本篇将详细介绍WPF 4 中 ...
随机推荐
- Hyperledger Fabric1.0环境搭建
一.准备CentOS系统,本文使用的是CentOS7.0 二.安装Docker 执行命令 yum -y install docker 验证是否安装成功 docker --version 三.安装Doc ...
- Java zip 压缩 文件夹删除,移动,重命名,复制
FileUtil.java import java.io.*; import java.util.List; import java.util.zip.ZipEntry; import java.ut ...
- Luogu-4022 [CTSC2012]熟悉的文章
广义后缀自动机+DP 对于作文库建出广义后缀自动机,广义自动机就是在每次添加一个字符串之前把\(last=0\),然后正常添加就好了 对于每个询问串,预处理出每个位置\(i\)能向前匹配的最长长度\( ...
- springcloud一些概念知识
1.Eureka 1)Eureka服务治理体系支持跨平台 2)三个核心概念:服务注册中心.服务提供者以及服务消费者 3)服务续约:注册完服务之后,服务提供者会维护一个心跳来不停的告诉Eureka Se ...
- springcloud-搭建服务注册中心
创建服务注册中心 1.创建一个springboot 命名为eureka-server 1)添加Eureka依赖 pom.xml <?xml version="1.0" enc ...
- 手把手教你安装SSL证书升级https
是不是觉得别人网站前面的小绿锁很好看? 而且,Google官方也正式承认过https是影响搜索排名的一个因素,那么如何将自己的网站全面升级为https呢?今天的内容就介绍一下如何将部署在Nginx的W ...
- Codeforces Round #374 (Div. 2) A , B , C 水,水,拓扑dp
A. One-dimensional Japanese Crossword time limit per test 1 second memory limit per test 256 megabyt ...
- 透透彻彻IoC
本文转载自:http://stamen.iteye.com/blog/1489223/ 引述:IoC(控制反转:Inverse of Control)是Spring容器的内核,AOP.声明式事务等功能 ...
- aodh M版本新特性 - Remove eventlet from Aodh in favour of threaded approach
在 Kilo版本, API WSGI application 可以有以下2种部署方式: As a Python command that runs a Werkzeug-based web serve ...
- Advanced SQL: Relational division in jOOQ
i Rate This Relational algebra has its treats. One of the most academic features is the ...