DataGrid控件是一个列表控件, 可以进行过滤,排序等。本文主要针对DataGrid的过滤功能进行分析, 并提供优化方案。
1)DataGrid的过滤过程:
用户输入过滤条件
调用DataGrid的CollectionViewSource的View.Refresh()功能
DataGrid控件内部调用CollectionView的RefreshOverride方法
CollectionView会调用CollectionViewSource的Filter回调函数来过滤符合自定义过滤条件的数据
CollectionView调用内部的OnCollectionChanged和OnCurrentChanged分别更新界面上的数据和当前选中的Item
2)通过分析发现(10W条数据, 实时过滤时UI非常卡,导致用户输入过滤字符丢失),调用CollectionViewSource的View.Refresh()的性能损耗主要集中于:
CollectionViewSource.Filter注册的方法,以及OnCollectionChanged(每次更新都导致ItemContainerGenerator重新构造UI元素)
3)优化方向:
减少CollectionViewSource.Filter注册的方法的耗时(在实时过滤中每个条件的更改都会调用Refresh从而调用Filter方法)
减少OnCollectionChanged调用的次数。
4)具体优化措施:
实例化3个Timer, 分别用于获取过滤后的数组(调用Filter)、调用OnCollectionChanged、OnCurrentItemChanged。3个timer分别由前一个timer完成时启动, 形成一个顺序操作。每次调用Timer时,先停止后续Timer的执行, 这样保证在合理的时间间隔里只有一次Refresh完整完成。
5)实现:
下面代码重载了ObservableCollection, 然后创建自定义的ListCollectionview.使用时只要用CustomCollection声明列表数据,包装为CollectionViewSource, 绑定到DataGrid的ItemSource即可。
//声明数组数据
public CustomCollection<StudyInfoModel> StudyList
{
get { return studyList; }
}
//包装为CollectionView
<CollectionViewSource Source="{Binding StudyList}" x:Key="StudyListView">
<CollectionViewSource.SortDescriptions>
<ComponentModel:SortDescription PropertyName="DateTime" Direction="Descending"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
//绑定到DataGrid
<DataGrid ItemsSource="{BindingMode=OneWay,Source={StaticResourceStudyListView}}" />
~~~
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);
}
}
~~~
- 前言
- win32与WPF的混合编程
- WPF: 一个可以用StoryBoard动态改变Grid行宽/列高的类
- MFC中调用WPF教程
- Expression Blend操作: 使用behavior来控制Storyboard
- WPF DatePicker 的textbox的焦点
- WPF 使用MultiBinding ,TwoWay ,ValidationRule ,需要注意的事项
- WPF TreeView 后台C#选中指定的Item, 需要遍历
- WPF GridViewColumn Sort DataTemplate
- DataGridColum的bug
- WPF Get Multibinding Expression, Update Source,
- WPF 后台触发 Validate UI‘s Element
- WPF ValidationRule 触发ErrorTemplate 的注意事项
- WPF DelegateCommand CanExecute
- WPF TextBox PreviewTextInput handle IME (chinese)
- No overload for &#39;OnStartup&#39; matches delegate &#39;System.Windows.StartupEventHandler&#39;
- WPF error: does not contain a static &#39;Main&#39; method suitable for an entry point
- WPF GridView中的CellTemplate失效的原因
- DataGrid 显示选中的item
- 如何得到WPF中控件绑定的EventTrigger
- 选中DataGrid的Cell而不是row
- ContextMenu的自定义
- 输入框只能输入英文
- TextBox的OnTextboxChanged事件里对Text重新赋值带中文, 导致崩溃
- DataGrid当列宽超出当前宽度时,没有数据也恒有滚动条
- wpf如何获取control template里的元素
- Set connectionId threw an exception.
- WPF中Visible设为Collapse时,VisualTreeHelper.GetChildrenCount为0
- XAML 编码规范 (思考)
- 如何为现有控件的DependencyProperty添加Value Changed事件?
- TreeView滚动TreeViewItem
- 为BindingList添加Sort
- WPF Background的设置有坑
- 自定义Panel中添加依赖属性需要注意的问题
- TextBlock截断字符显示为....
- DataGrid 支持字符截断显示
- TreeView控件实践
- WPF如何更改系统控件的默认高亮颜色 (Highlight brush)
- ViewModel中C# Property自动添加OnPropertyChanged处理的小工具, 以及相应Python知识点
- WPF中Xaml编译正常而Designer Time时出错的解决办法
- 关于Snoop的用法
- wpf中为DataGrid添加checkbox支持多选全选
- WPF中DataGrid控件的过滤(Filter)性能分析及优化
- wpf控件提示Value ‘’ can not convert
- DropShadowEffect导致下拉框控件抖动
- 再论WPF中的UseLayoutRounding和SnapsToDevicePixels
- WPF案例:如何设计历史记录查看UI
- WPF案例:如何设计搜索框(自定义控件的原则和方法)
- WPF基本概念入门
- WPF开发中Designer和码农之间的合作
- 聊聊WPF中的Dispatcher
- 聊聊WPF中字体的设置
- Bug:DataGridCell的显示不完整
- WPF中ToolTip的自定义
- WPF中ItemsControl绑定到Google ProtocolBuffer的结构体时的性能问题
- TreeView的性能问题
- Xaml中string(字符串)常量的定义以及空格的处理
- 依赖属性
- WPF中的CheckBox的_ (underscore / 下划线)丢失
- WPF错误:必须使“Property”具有非 null 值。
- WPF中ItemsControl应用虚拟化时找到子元素的方法
- WPF毫秒级桌面时钟的实现-C#中Hook(钩子)的应用
- KB2464222导致IsNonIdempotentProperty方法找不见
- WPF中PreviewMouseDownEvent的系统处理:TabItem的PreviewMouseDown 事件弹框后不切换的问题调查
- WPF文字渲染相关的问题及解决
- wpf中的默认右键菜单中的复制、粘贴、剪贴等没有本地化的解决方案
- WPF内部DeliverEvent读锁和PrivateAddListener写锁导致死锁
- Windbg调试WPF的依赖属性
- WPF 后台Render线程崩溃, Exception from HRESULT: 0x88980406
- WPF中DependencyObject与DependencyProperty的源码简单剖析
- 禁用WPF中DataGrid默认的鼠标左键拖动多选行的效果
- wpf工程中在Xaml文件下添加多个cs文件
- ScrollViewer滚动到底来触发加载数据的Behavior