如何在替换所有元素或添加元素集合时避免多次触发ObservableCollection.CollectionChanged

如何在替换所有元素或添加元素集合时避免多次触发ObservableCollection.CollectionChanged,第1张

如何在替换所有元素添加元素集合时避免多次触发ObservableCollection.CollectionChanged

ColinE提供的所有信息都是正确的。我只想添加

ObservableCollection
用于此特定情况的子类

public class SmartCollection<T> : ObservableCollection<T> {    public SmartCollection()        : base() {    }    public SmartCollection(IEnumerable<T> collection)        : base(collection) {    }    public SmartCollection(List<T> list)        : base(list) {    }    public void AddRange(IEnumerable<T> range) {        foreach (var item in range) { Items.Add(item);        }        this.onPropertyChanged(new PropertyChangedEventArgs("Count"));        this.onPropertyChanged(new PropertyChangedEventArgs("Item[]"));        this.onCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));    }    public void Reset(IEnumerable<T> range) {        this.Items.Clear();        AddRange(range);    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5559704.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存