如何在 wpf 数据绑定发生时显示加载图形/动画
我有一个包含 DataGrid
的 WPF 用户控件。我将视图模型的 ObservableCollection
绑定到它。每个视图模型都有另一个视图模型集合,我用它们来绑定另一个 DataGrid
。因此,效果是一个 DataGrid
,其中包含行详细信息模板中的嵌套 DataGrid
。
通常,绑定速度非常快,但有时当有大量数据时,它可能会在绑定/绘图发生时挂起 UI。
有没有一种方法可以在绑定/绘图正在进行时显示加载动画或进度条?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有同样的问题,这就是我解决它的方法。
我发现 DataGrid 仅在显示网格时才开始创建控件。就我而言,这是一个耗时的过程。经过一番跟踪后,我发现创建控件是在测量期间发生的!
我的解决方案是重写 MeasureOverride 并将等待光标放在基类调用周围。我将等待光标设置封装在一个类中。所以代码看起来像这样。
I had the same problem and this is how I solved it.
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 !
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.
可能有一个更正式的,或者至少更简单的解决方案,但是您可以使用模式弹出窗口,该窗口显示在工作线程中,并在网格完成加载时异步关闭:
您可以用类似加载栏的东西替换 TextBlock ,并且您可以通过参数化处理网格加载的对象并将其传递给常用方法来使代码可重用。
我希望这对你有用。
There's probably a more formal, or at least simpler solution, but you could use a modal popup window that is shown in a worker thread and is closed asynchronously when your is grid done loading:
You can replace the TextBlock with something pretty like a loading bar, and you could make the code re-usable by parameterizing the object that handles the loading of the grid(s) and passing it in to a commonly used method.
I hope that works for you.