发生 wpf 数据绑定时如何显示加载图形/动画 [英] How to show a loading graphic/animation when wpf data binding is taking place
问题描述
我有一个包含 DataGrid
的 WPF 用户控件.我将视图模型的 ObservableCollection
绑定到它.每个视图模型都有另一个视图模型集合,我用它们来绑定另一个 DataGrid
.所以效果是一个 DataGrid
,其中包含一个嵌套的 DataGrid
包含在行详细信息模板中.
I have a WPF user control that contains a DataGrid
. I'm binding an ObservableCollection
of view models to it. Each view model has another collection of view models that I'm using to bind another DataGrid
to. So the effect is a DataGrid
with a nested DataGrid
contained in the row details template.
通常绑定非常快,但有时当有大量数据时,它可以在绑定/绘图发生时挂起 UI.
Normally the binding is quite quick, but sometimes when there's a lot of data it can hang the UI while the binding/drawing is taking place.
有没有一种方法可以在绑定/绘制过程中显示加载动画或进度条?
Is there a way where I can either show a loading animation or progress bar while the binding/drawing is in progress?
推荐答案
我遇到了同样的问题,我就是这样解决的.
I had the same problem and this is how I solved it.
我发现 DataGrid 只会在显示网格时才开始创建控件.就我而言,这是一个耗时的过程.经过一些跟踪,我发现创建控件是在测量过程中发生的!
I discovered that DataGrid will only start creating controls when it displays the grid. In my case this was the time consuming process. After some tracing I found that creating the controls happens during measuring !
我的解决方案是覆盖 MeasureOverride 并将等待光标放在基类调用周围.我将我的等待光标设置封装在一个类中.所以代码看起来像这样.
My solution is to override MeasureOverride and put the wait cursor around the base class call. I encapsulated my wait cursor setting in a class. So the code looks like this.
protected override Size MeasureOverride(Size availableSize)
{
using (new DisposableWaitCursor(this))
{
return base.MeasureOverride(availableSize);
}
}
这篇关于发生 wpf 数据绑定时如何显示加载图形/动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!