ObservableCollection在WPF ListView中不更新

22 浏览
0 Comments

ObservableCollection在WPF ListView中不更新

我创建了一个名为ObservableCollectionEx.cs的类,该类继承自ObservableCollection类,在更新集合时抑制通知,直到更新完成,参考此处的答案。

这个类的代码如下:

public class ObservableCollectionEx : ObservableCollection
    {
        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);
        }
    }

在一个类中创建了一个模型集合,该类旨在根据多个事件进行更新。其中一个事件基于可观察序列,它仅在间隔时间内更新集合,而另一个事件是基于按钮点击事件的。在逐步执行代码时,我发现两个事件都成功导致集合更新,但只有按钮点击才会通知并相应地更新WPF的ListView。UI是一个WPF的UserControl,它用于在Microsoft Word中使用VSTO创建一个CustomTaskPane

通过Observable序列更新集合的代码:

public partial class CrossReferenceControl : UserControl, ICrossReferenceControl
    {
        private ICrossReferenceControlViewModel referenceControlViewModel;
        private IOpenDocumentModel OpenDocumentModel;
        private ICrossReferenceGuy CrossReferenceGuy;
        private bool isOpen;
        private IObservable openDocModelUpdateObservable;
        private static TimeSpan period = TimeSpan.FromSeconds(20);
        private IObservable observable = Observable.Interval(period);
        public readonly Subject OpenDocModelUpdateActionSubject = new Subject();
        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));
        }
}

通过按钮点击事件更新的代码(这个工作正常):

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

注意:代码被精简以提供我认为是必要的部分。

0