暂停对绑定的ObservableCollection< T>的DataGrid的更新

编程入门 行业动态 更新时间:2024-10-28 16:20:34
本文介绍了暂停对绑定的ObservableCollection< T>的DataGrid的更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有办法暂停 ObservableCollection 的 NotifyCollectionChanged 事件?我想到如下所示:

Is there a way to pause the NotifyCollectionChanged event of an ObservableCollection? I thought something like the following:

public class PausibleObservableCollection<Message> : ObservableCollection<Message> { public bool IsBindingPaused { get; set; } protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (!IsBindingPaused) base.OnCollectionChanged(e); } }

这样会暂停通知, out(但仍然添加)项目在 NotifyCollectionChangedEventArgs 内,因此在我再次启用通知时不会传递到绑定的DataGrid。

This pauses the notification indeed, but obviously the then left out (but still added) items are within the NotifyCollectionChangedEventArgs and are therefore not passed to the bound DataGrid when I enable the notification again.

为了控制这个方面,我必须提出一个集合的自定义实现吗?

Will I have to come up with a custom implementation of a collection in order to control this aspect?

推荐答案

如果您不想松开任何通知,临时存储可能正常工作,以下可能会工作,但未经测试:

If you do not want to loose any notifications a temporary storage might work, the following might work but is untested:

public class PausibleObservableCollection<T> : ObservableCollection<T> { private readonly Queue<NotifyCollectionChangedEventArgs> _notificationQueue = new Queue<NotifyCollectionChangedEventArgs>(); private bool _isBindingPaused = false; public bool IsBindingPaused { get { return _isBindingPaused; } set { _isBindingPaused = value; if (value == false) { while (_notificationQueue.Count > 0) { OnCollectionChanged(_notificationQueue.Dequeue()); } } } } protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { if (!IsBindingPaused) base.OnCollectionChanged(e); else _notificationQueue.Enqueue(e); } }

这应该推动收集时发生的每一个变化暂停进入队列,然后在收集集恢复后将其清空。

This should push every change that happens while the collection is paused into a queue, which then is emptied once the collection is set to resume.

更多推荐

暂停对绑定的ObservableCollection&lt; T&gt;的DataGrid的更新

本文发布于:2023-11-07 15:07:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566779.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   ObservableCollection   amp   DataGrid   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!