WPF 4 数据网格刷新缓慢

发布于 2024-11-07 04:54:55 字数 724 浏览 0 评论 0 原文

我正在使用 .NET 4 框架中的 WPF DataGrid 组件,它绑定到我在这里找到的线程安全的可观察集合: http://www.deanchalk.me.uk/post/Thread-Safe-Dispatcher-Safe-Observable-Collection-for-WPF.aspx

该程序是一个系统管理工具,可 ping范围内的每个 IP 地址,如果有响应,它会在集合中创建一个对象,其中包含来自计算机的一些详细信息。

我遇到的问题是性能不佳。最初,我只是更新集合并让数据网格接收更改。这导致了一个问题,除非我滚动,否则 DataGrid 控件不会刷新并显示数据。

所以我添加了一个计时器来调用网格刷新方法,计时器每 750 毫秒计时一次。这非常有效,直到我意识到当定时器启用并且正在扫描时,程序 UI 完全没有响应。

没有计时器,性能还可以接受,没有它,就很糟糕了。我尝试了几个超时时间值(最多 2000 毫秒),但没有成功,并且还确保我的列是固定宽度的(我读到自动生成的列和宽度可能会导致性能问题)。

网格中的行数约为 300 - 400 个,有 5 列,因此它并不是一个巨大的网格。

有谁对我如何在添加后面的集合时使网格及时更新而不牺牲性能有任何建议?

I am using the WPF DataGrid component from the .NET 4 framework, it is bound to a thread safe observable collection which I found here : http://www.deanchalk.me.uk/post/Thread-Safe-Dispatcher-Safe-Observable-Collection-for-WPF.aspx

The program is a System Admin tool which pings each IP Address in a range, if there is a response it creates an object in the collection with some details from the computer.

The problem I am having, is poor performance. Initially, I was just updating the Collection and letting the datagrid pick up the changes. This caused an issue where the DataGrid control wasn't refreshing and showing the data unless I scrolled.

So I added a timer to call the grids refresh method, the timer ticks every 750ms. This worked brilliantly, until I realised that the programs UI is completely unresponsive while the timer is enabled and it is scanning.

Without the timer, performance is acceptable, without it, its terrible. I have tried several values for the timeout period (up to 2000ms) without any luck and have also made sure that my columns are fixed width (I read that autogenerated columns and widths can cause performance issues).

The amount of Row's in the grid is about 300 - 400 with 5 columns, so its not exactly a huge grid.

Does anyone have any suggestions of how I could get the grid to update in a timely manner when the collection behind is added to, without sacrificing performance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2024-11-14 04:54:55

如果您的绑定正确,您将不需要手动刷新 DataGrid。更新场景后面的可观察集合应该自动触发(假设您已经实现了 NotifyPropertyChanged)DataGrid 的更新。 300 - 400 并不是一个会减慢 DataGrid 速度的数字。你的DataGrid在ScrollViewer里面吗?有时这也会降低性能。确保您的绑定工作正常,然后您就不需要手动刷新。

If your bindings are correct you won't need to refresh the DataGrid manually. Updating the observable collection behind the scene should automatically trigger(assuming you have implemented NotifyPropertyChanged) the update of DataGrid. 300 - 400 is not a number that will slow down the DataGrid. Is your DataGrid inside ScrollViewer? sometimes that degrades the performance as well. Make sure your bindings are working fine then you won't need manual refresh.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文