我想做的是FreezableCollection.AddRange(collectionToAdd) [英] What I want to do is FreezableCollection.AddRange(collectionToAdd)

查看:106
本文介绍了我想做的是FreezableCollection.AddRange(collectionToAdd)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是 FreezableCollection.AddRange(collectionToAdd)



每次我添加到FreezableCollection一个事件被提出,一些事情发生。现在我有一个新的集合,我想添加,但这一次我想让FreezableCollection的 CollectionChanged事件只触发一次。



循环通过和添加它们将为每个新项目引发事件。



有一种方法,我可以添加到FreezableCollection的目标,类似List.AddRange? p>

解决方案

从集合中导出并覆盖您要更改的行为。我可以这样做:

  public class PrestoObservableCollection< T> :ObservableCollection< T> 
{
private bool _delayOnCollectionChangedNotification {get;组; }

///< sum>
///向可观察集合中添加一系列IEnumerable项,并且可选地延迟通知,直到操作完成。
///< / summary>
///< param name =itemsToAdd>< / param>
public void AddRange(IEnumerable< T> itemsToAdd)
{
if(itemsToAdd == null)throw new ArgumentNullException(itemsToAdd);

if(itemsToAdd.Any()== false){return; } //无法添加。

_delayOnCollectionChangedNotification = true;

try
{
foreach(itemsToAdd中的T项){this.Add(item); }
}
finally
{
ResetNotificationAndFireChangedEvent();
}
}

///< summary>
///清除ObservableCollection中的项目,并且可选地延迟通知,直到操作完成。
///< / summary>
public void ClearItemsAndNotifyChangeOnlyWhenDone()
{
try
{
if(!this.Any()){return; } //没有可用的删除。

_delayOnCollectionChangedNotification = true;

this.Clear();
}
finally
{
ResetNotificationAndFireChangedEvent();
}
}

///< summary>
///覆盖ObservableCollection类上的虚拟OnCollectionChanged()方法。
///< / summary>
///< param name =e>事件参数< / param>
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if(_delayOnCollectionChangedNotification){return; }

base.OnCollectionChanged(e);
}

private void ResetNotificationAndFireChangedEvent()
{
//关闭延迟通知并调用OnCollectionChanged()方法,并告诉我们集合中有变化。
_delayOnCollectionChangedNotification = false;
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}


What I want to do is FreezableCollection.AddRange(collectionToAdd)

Each time I add to the FreezableCollection an event is raised and something happens. Now I have a new collection I would want to add but this time I want the CollectionChanged event of the FreezableCollection to fire only once.

Looping through and adding them will raise the event for each new item.

Is there a way I can add to FreezableCollection all at a goal, similar to List.AddRange?

解决方案

Derive from a collection and override the behavior that you'd like to change. I was able to do it this way:

public class PrestoObservableCollection<T> : ObservableCollection<T>
    {
        private bool _delayOnCollectionChangedNotification { get; set; }

        /// <summary>
        /// Add a range of IEnumerable items to the observable collection and optionally delay notification until the operation is complete.
        /// </summary>
        /// <param name="itemsToAdd"></param>
        public void AddRange(IEnumerable<T> itemsToAdd)
        {
            if (itemsToAdd == null) throw new ArgumentNullException("itemsToAdd");

            if (itemsToAdd.Any() == false) { return; }  // Nothing to add.

            _delayOnCollectionChangedNotification = true;            

            try
            {
                foreach (T item in itemsToAdd) { this.Add(item); }
            }
            finally
            {
                ResetNotificationAndFireChangedEvent();
            }
        }

        /// <summary>
        /// Clear the items in the ObservableCollection and optionally delay notification until the operation is complete.
        /// </summary>
        public void ClearItemsAndNotifyChangeOnlyWhenDone()
        {
            try
            {
                if (!this.Any()) { return; }  // Nothing available to remove.

                _delayOnCollectionChangedNotification = true;

                this.Clear();
            }
            finally
            {
                ResetNotificationAndFireChangedEvent();
            }
        }

        /// <summary>
        /// Override the virtual OnCollectionChanged() method on the ObservableCollection class.
        /// </summary>
        /// <param name="e">Event arguments</param>
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (_delayOnCollectionChangedNotification) { return; }

            base.OnCollectionChanged(e);
        }

        private void ResetNotificationAndFireChangedEvent()
        {
            // Turn delay notification off and call the OnCollectionChanged() method and tell it we had a change in the collection.
            _delayOnCollectionChangedNotification = false;
            this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }
    }

这篇关于我想做的是FreezableCollection.AddRange(collectionToAdd)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆