ItemsControl.ItemsSource MVVM performance

后端 未结 2 429
眼角桃花
眼角桃花 2020-12-29 17:22

I have an (non-virtualized) ItemsControl that binds its ItemsSource to a ObeservableCollection of ViewModel instances. Now once the large amount Model instances is loaded al

2条回答
  •  醉话见心
    2020-12-29 17:48

    You can create a a class derived from ObservableCollection which allows you to temporarily suspend CollectionChanged events like this:

    public class SuspendableObservableCollection : ObservableCollection
    {
        private bool suspended;
    
        public bool Suspended 
        {
            get
            {
                return this.suspended;
            }
            set
            {
                this.suspended = value;
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(
                    NotifyCollectionChangedAction.Reset));
            }
        }
    
        protected override void OnCollectionChanged(
            NotifyCollectionChangedEventArgs args)
        {
           if (!Suspended)
           {
               base.OnCollectionChanged(args);
           }
        }
    }
    

提交回复
热议问题