简体   繁体   English

取消可观察集合上的集合更改事件

[英]Cancel collection changed event on an observable collection

How can i cancel the collection changed event on an observable collection? 如何取消可观察集合上的集合更改事件? When my collection changes it invokes methods on a third party dll.These methods may or may not fail. 当我的集合更改时,它会调用第三方dll上的方法。这些方法可能会也可能不会失败。

If they fail, i want dont want the item to be added to or removed from the collection. 如果它们失败了,我不希望该项目被添加到集合中或从集合中删除。 Looking at the name, it looks like the collection changed event is fired after something has been added or deleted, but how could i achieve my functionality? 从名称上看,添加或删除某些内容后似乎触发了更改集合事件,但是我如何实现我的功能?

Too late but it might help someone else : 为时已晚,但可能会对其他人有所帮助:

class ExtendedObservableCollection<T> : ObservableCollection<T>
{
    private bool _suppressNotification = false;

    public bool AllowNotifications { get { return _suppressNotification; } }

    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        if (!_suppressNotification)
            base.OnCollectionChanged(e);
    }

    protected override void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (!_suppressNotification)
            base.OnPropertyChanged(e);
    }

    public void ActivateNotifications()
    {
        _suppressNotification = false;
    }

    public void DesactivateNotifications()
    {
        _suppressNotification = true;
    }

    public void AddRange(IEnumerable<T> list)
    {
        if (list == null)
            throw new ArgumentNullException("list");

        _suppressNotification = true;

        foreach (T item in list)
        {
            Add(item);
        }
        _suppressNotification = false;
        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add));
    }


}

I'll share the homely hack I came up with for my WPF/MVVM solution. 我将分享我为WPF / MVVM解决方案想到的家常便饭。 In the methods I call for e.Action == NotifyCollectionChangedAction.Remove or e.Action == NotifyCollectionChangedAction.Add I check for failure and take any actions required to undo the change and set a boolean member _updateObservable to true. 在这些方法中,我要求e.Action == NotifyCollectionChangedAction.Removee.Action == NotifyCollectionChangedAction.Add我检查失败并采取撤消更改所需的任何操作,并将布尔成员_updateObservable设置为true。 Since I can't modify the ObservableCollection during the change event, I have to defer it. 由于在change事件期间无法修改ObservableCollection,因此必须推迟它。 Setting the boolean seemed like the easiest thing. 设置布尔值似乎是最简单的事情。

Then in the view model I have a property used for binding the selected item on the observable. 然后,在视图模型中,我具有用于将所选项目绑定到可观察对象上的属性。 I added to that property get method if (_updateObservable) UpdateObservable(); 我添加到该属性的get方法, if (_updateObservable) UpdateObservable(); It appears the selected item bound property always fires its getter even if the item added or deleted does not directly affect the selected item. 即使添加或删除的项目不会直接影响所选的项目,所选项目的绑定属性似乎总是​​会触发其吸气剂。 UpdateObservable() adds back or removes any items required from the collection and sets the flag to false. UpdateObservable()从集合中添加或删除任何必需的项,并将标志设置为false。

您可以使用new命令简单地覆盖该特定方法。

If you can manage to handle the event in your code, NotifyCollectionChangedEventArgs.NewItems returns an IList of the new items involved in the change. 如果可以在代码中管理事件,则NotifyCollectionChangedEventArgs.NewItems返回更改所涉及的新项目的IList You could then remove these items from the collection if the methods in the third party dll failed. 如果第三方dll中的方法失败,则可以从集合中删除这些项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM