ObservableCollection Doesn't support AddRange method, so I get notified for each item added, besides what about INotifyCollectionChanging?

前端 未结 12 1417
走了就别回头了
走了就别回头了 2020-11-22 16:19

I want to be able to add a range and get updated for the entire bulk.

I also want to be able to cancel the action before it\'s done (i.e. collection changing besides

12条回答
  •  臣服心动
    2020-11-22 16:48

    The C# summarized descendant.

    More reading: http://blogs.msdn.com/b/nathannesbit/archive/2009/04/20/addrange-and-observablecollection.aspx

    public sealed class ObservableCollectionEx : ObservableCollection
    {
        #region Ctor
    
        public ObservableCollectionEx()
        {
        }
    
        public ObservableCollectionEx(List list) : base(list)
        {
        }
    
        public ObservableCollectionEx(IEnumerable collection) : base(collection)
        {
        }
    
        #endregion
    
        ///  
        /// Adds the elements of the specified collection to the end of the ObservableCollection(Of T). 
        ///  
        public void AddRange(
            IEnumerable itemsToAdd,
            ECollectionChangeNotificationMode notificationMode = ECollectionChangeNotificationMode.Add)
        {
            if (itemsToAdd == null)
            {
                throw new ArgumentNullException("itemsToAdd");
            }
            CheckReentrancy();
    
            if (notificationMode == ECollectionChangeNotificationMode.Reset)
            {
                foreach (var i in itemsToAdd)
                {
                    Items.Add(i);
                }
    
                OnPropertyChanged(new PropertyChangedEventArgs("Count"));
                OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    
                return;
            }
    
            int startIndex = Count;
            var changedItems = itemsToAdd is List ? (List) itemsToAdd : new List(itemsToAdd);
            foreach (var i in changedItems)
            {
                Items.Add(i);
            }
    
            OnPropertyChanged(new PropertyChangedEventArgs("Count"));
            OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, changedItems, startIndex));
        }
    
        public enum ECollectionChangeNotificationMode
        {
            /// 
            /// Notifies that only a portion of data was changed and supplies the changed items (not supported by some elements,
            /// like CollectionView class).
            /// 
            Add,
    
            /// 
            /// Notifies that the entire collection was changed, does not supply the changed items (may be inneficient with large
            /// collections as requires the full update even if a small portion of items was added).
            /// 
            Reset
        }
    }
    

提交回复
热议问题