我可以以某种方式暂时禁用WPF数据绑定更改吗?

Tow*_*wer 17 c# data-binding wpf mvvm

我有一个使用MVVM数据绑定的WPF应用程序.我正在为一个ObservableCollection<...>和其中很多人添加项目.

现在我想知道每次我在集合中添加一个,是否会立即触发事件并导致不必要的开销?如果是这样,我可以以某种方式暂时禁用事件通知并在我的代码结束时手动触发一次,这样如果我添加10k项,它只会被触发一次,而不是10k次?

更新:我尝试过这门课程:

using System;
using System.Linq;
using System.Collections.Specialized;
using System.Collections.Generic;

namespace MyProject
{

    /// <summary> 
    /// Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed. 
    /// </summary> 
    /// <typeparam name="T"></typeparam> 
    public class ObservableCollection<T> : System.Collections.ObjectModel.ObservableCollection<T>
    {

        /// <summary> 
        /// Adds the elements of the specified collection to the end of the ObservableCollection(Of T). 
        /// </summary> 
        public void AddRange(IEnumerable<T> collection)
        {
            foreach (var i in collection) Items.Add(i);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, collection.ToList()));
        }

        /// <summary> 
        /// Removes the first occurence of each item in the specified collection from ObservableCollection(Of T). 
        /// </summary> 
        public void RemoveRange(IEnumerable<T> collection)
        {
            foreach (var i in collection) Items.Remove(i);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, collection.ToList()));
        }

        /// <summary> 
        /// Clears the current collection and replaces it with the specified item. 
        /// </summary> 
        public void Replace(T item)
        {
            ReplaceRange(new T[] { item });
        }
        /// <summary> 
        /// Clears the current collection and replaces it with the specified collection. 
        /// </summary> 
        public void ReplaceRange(IEnumerable<T> collection)
        {
            List<T> old = new List<T>(Items);
            Items.Clear();
            foreach (var i in collection) Items.Add(i);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, collection.ToList()));
        }

        /// <summary> 
        /// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class. 
        /// </summary> 
        public ObservableCollection() : base() { }

        /// <summary> 
        /// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class that contains elements copied from the specified collection. 
        /// </summary> 
        /// <param name="collection">collection: The collection from which the elements are copied.</param> 
        /// <exception cref="System.ArgumentNullException">The collection parameter cannot be null.</exception> 
        public ObservableCollection(IEnumerable<T> collection) : base(collection) { }
    }
}
Run Code Online (Sandbox Code Playgroud)

我现在收到此错误:

附加信息:不支持范围操作.

错误来自:

OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, collection.ToList()));
Run Code Online (Sandbox Code Playgroud)

Dr.*_*ABT 14

一种非常快速简便的方法是在调用AddRange时子类化ObservableCollection并挂起通知.有关说明,请参阅以下博客文章.


Xia*_* Ge 14

ObservableCollection的这个扩展很容易解决问题.

它公开了一个公共SupressNotification属性,允许用户控制何时抑制CollectionChanged通知.

它不提供范围插入/删除,但如果抑制了CollectionChanged通知,则在大多数情况下,对集合执行范围操作的需求会减少.

此实现使用重置通知替换所有已抑制的通知.这在逻辑上是合理的.当用户禁止通知,进行批量更改然后重新启用通知时,应该发送重新发送通知.

public class ObservableCollectionEx<T> : ObservableCollection<T>
{
    private bool _notificationSupressed = false;
    private bool _supressNotification = false;
    public bool SupressNotification
    {
        get
        {
            return _supressNotification;
        }
        set
        {
            _supressNotification = value;
            if (_supressNotification == false && _notificationSupressed)
            {
                this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                _notificationSupressed = false;
            }
        }
    }

    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        if (SupressNotification)
        {
            _notificationSupressed = true;
            return;
        }
        base.OnCollectionChanged(e);
    }
}
Run Code Online (Sandbox Code Playgroud)


Tig*_*ran 8

有一种"棘手"的方式,但在我看来,实现这一点非常准确.是写你自己ObservableCollection并实施AddRange处理.

这样,您可以将您的所有10K元素融入一些"持有人集"之后,一周时间你完成,使用AddRange ObservableColleciton做到这一点.

有关此内容的更多信息,请访问此链接:

ObservableCollection不支持AddRange方法....

或者这个

AddRange和ObservableCollection


归档时间:

查看次数:

15278 次

最近记录:

7 年,8 月 前