暂停对绑定 ObservableCollection的 DataGrid 的更新

发布于 2024-10-29 02:43:32 字数 621 浏览 6 评论 0原文

有没有办法暂停notifyCollectionChanged observableCollection的事件?我认为以下内容:

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);
    }
}

这确实暂停了通知,但是显然,当时被排除的(但仍添加)的项目在notifyCollectionChangeDeventargs 中,因此当我启用通知时未传递到bond datagrid再次。

为了控制这一方面,我是否必须提出定制的集合实现?

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);
    }
}

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?

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

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

发布评论

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

评论(2

把时间冻结 2024-11-05 02:43:32

如果您不想丢失临时存储可能起作用的任何通知,则以下方法可能有效,但未经测试:

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);
    }
}

这应该将集合暂停时发生的每个更改推送到队列中,一旦集合设置为恢复,队列就会被清空。

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.

一梦等七年七年为一梦 2024-11-05 02:43:32

为了配合@HB的答案(我在他/她发布时进行测试) - 您可以通过 NotifyCollectionChangedAction.Reset 将操作更改为 CollectionChanged 事件的事件参数。请注意,这对于大型集合来说效率不高。

public class PausibleObservableCollection<T> : ObservableCollection<T>
{
    private bool _isPaused = false;
    public bool IsPaused 
    { 
      get { return _isPaused; } 
      set 
      { 
          _isPaused = value;
          if (!value)
          { this.OnCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(System.Collections.Specialized.NotifyCollectionChangedAction.Reset)); }

      } 
    }

    protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (!IsPaused)
        { base.OnCollectionChanged(e); }
    }
}

To go along with @H.B's answer (I was testing while he/she posted) - you can pass the NotifyCollectionChangedAction.Reset Change Action as an event argument to the CollectionChanged event. Note that this will not be efficient on large collections.

public class PausibleObservableCollection<T> : ObservableCollection<T>
{
    private bool _isPaused = false;
    public bool IsPaused 
    { 
      get { return _isPaused; } 
      set 
      { 
          _isPaused = value;
          if (!value)
          { this.OnCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(System.Collections.Specialized.NotifyCollectionChangedAction.Reset)); }

      } 
    }

    protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (!IsPaused)
        { base.OnCollectionChanged(e); }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文