0

I created an ObservableCollectionEx.cs class that inherits the ObservableCollection class to suppress notifications while the collection is being updated until it's done updating from the answer here.

The class:

public class ObservableCollectionEx<T> : ObservableCollection<T>
    {
        private bool _notificationSupressed = false;
        private bool _supressNotification = false;
        public bool SupressNotification
        {
            get
            {
                return _supressNotification;
            }
            set
            {
                _supressNotification = value;
                if (_supressNotification == false && _notificationSupressed)
                {
                    this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                    _notificationSupressed = false;
                }
            }
        }

        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (SupressNotification)
            {
                _notificationSupressed = true;
                return;
            }
            base.OnCollectionChanged(e);
        }
    }

A collection of models is created in a class that is meant to update in response to a number of events. One is based on an observable sequence that simply updates the collection at an interval and another is based on a button click event. Stepping through the codes, I see that both events are causing the collection to update successfully, but only the button click causes the WPF ListView to be notified and updated accordingly. The UI is a WPF UserControl that is used to create a CustomTaskPane in Microsoft Word using VSTO.

The code that updates the collection via Observable sequence:

 public partial class CrossReferenceControl : UserControl, ICrossReferenceControl
    {
        private ICrossReferenceControlViewModel referenceControlViewModel;
        private IOpenDocumentModel OpenDocumentModel;
        private ICrossReferenceGuy CrossReferenceGuy;
        private bool isOpen;

        private IObservable<bool> openDocModelUpdateObservable;

        private static TimeSpan period = TimeSpan.FromSeconds(20);

        private IObservable<long> observable = Observable.Interval(period);

        public readonly Subject<bool> OpenDocModelUpdateActionSubject = new Subject<bool>();

        public ICrossReferenceControlViewModel ReferenceControlViewModel => referenceControlViewModel;
        public bool IsOpen 
        {
            get { return isOpen; }
            set { isOpen = value; }
        }
        public CrossReferenceControl(IOpenDocumentModel openDocumentModel, ICrossReferenceControlViewModel referenceControlViewModel, ICrossReferenceGuy crossReferenceGuy)
        {
            InitializeComponent();
            this.referenceControlViewModel = referenceControlViewModel;
            OpenDocumentModel = openDocumentModel;
            CrossReferenceGuy = crossReferenceGuy;
            //CrossReferenceControlViewModel controlViewModel = new CrossReferenceControlViewModel((OpenDocumentModel)openDocumentModel);
            DataContext = referenceControlViewModel;
            observable.Subscribe(O => OpenDocumentModel.UpdateCaptionsSubject.OnNext(IsOpen));
        }
}

The code that updates via button click event (this works fine):

        private void ButtonRefresh_Click(object sender, RoutedEventArgs e)
        {
            OpenDocumentModel.UpdateCaptionsSubject.OnNext(IsOpen);
        }

Note: The codes are cut down to provide only what I think is essential.

  • Two obvious questions. Does it work with a regular ObservableCollection? Why do you think the UI should update when you suppress notifications? – Clemens Nov 27 '22 at 08:41
  • @Clemens, I did not try it with a regular ObservableCollection. To update the collection, I usually clear it first before using a generic `List<>` to repopulate it. This is where the notification is suppressed to make it unnoticeable to the user. The suppressed notification is enabled immediately after the collection is repopulated and the UI is updated if this was effected via button click but not the other way. Maybe I should try the normal ObservableCollection, anyway, to see if suppressing the notification is contributing to the inconsistent behaviour. – nomacreates Nov 27 '22 at 08:57
  • It is the same behaviour even with the regular ObservableCollection. – nomacreates Nov 27 '22 at 11:51

0 Answers0