Adding a range of values to an ObservableCollection efficiently

前端 未结 4 1282
Happy的楠姐
Happy的楠姐 2020-12-24 06:29

I have an ObservableCollection of items that is bound to a list control in my view.

I have a situation where I need to add a chunk of values to the star

4条回答
  •  不思量自难忘°
    2020-12-24 07:07

    This answer didn't show me the new entries in a DataGrid. This OnCollectionChanged works for me:

    public class SilentObservableCollection : ObservableCollection
    {
        public void AddRange(IEnumerable enumerable)
        {
            CheckReentrancy();
    
            int startIndex = Count;
    
            foreach (var item in enumerable)
                Items.Add(item);
    
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, new List(enumerable), startIndex));
            OnPropertyChanged(new PropertyChangedEventArgs("Count"));
            OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
        }
    }
    

提交回复
热议问题