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 中 ...
随机推荐
- 内核模块编译时怎样绕过insmod时的版本检查
1.Uboot:每个arm芯片或者海斯芯片都有各自的uboot. 2.但他们的内核版本可以是一样的,主要是跟各自内核的进行的编译选项有关, 31的内核版本里加了版本检查选项“Kernel type-& ...
- python中初始化实例属性
虽然我们可以自由地给一个实例绑定各种属性,但是,现实世界中,一种类型的实例应该拥有相同名字的属性.例如,Person类应该在创建的时候就拥有 name.gender 和 birth 属性,怎么办? 在 ...
- python对象类型----数字&字符串
一数据类型: float: 1.3e-3 1.3*10的负三次方 print (1.3e-3) bin() #转换为二进进制 oct() #转换为8进制 hex()#转 ...
- Linux FTP 上传一键脚本
下面来介绍一下这个 FTP 上传一键脚本 ftp_upload.sh. 用途:用于在Linux系统下搭建FTP客户端向FTP服务器端上传文件: 总结一下 ftp_upload.sh 特点:1.支持文件 ...
- 语义web相关概念
前言:最近做的项目是自然语言处理相关的,看了一本书<语义web技术基础>,总的来看,接触自然语言处理,语义理解也有差不多一年的时间了.这两天想了一想,自己究竟学到了什么,掌握了哪些新的知识 ...
- C#反射第一天
[转]C#反射 反射(Reflection)是.NET中的重要机制,通过放射,可以在运行时获得.NET中每一个类型(包括类.结构.委托.接口和枚举等)的成员,包括方法.属性.事件,以及构造函数等. ...
- How to use Jenkins
一.关键点 1.how to start the build server? do i need to start some app to do this? I don't believe so... ...
- js适配器模式
适配器模式,将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 系统的数据和行为都正确,但接口不符时,我们应该考虑用适配器,目的是使控制范 ...
- 目标检测 — NMS
1.非极大值抑制步骤 非极大值抑制算法(Non-maximum suppression,NMS)在目标检测中经常用到.我们的检测算法可能对同一目标产生多次检测的结果,非极大值抑制算法可以保证每个目标只 ...
- 解决: PyInstaller打包后exe文件打开时出现failed to execute script
def resource_path(self, relative): if hasattr(sys, "_MEIPASS"): return os.path.join(sys._M ...