在 wpf 中的 observablecollection 中的集合更改之前做一些事情

发布于 2024-11-15 03:05:48 字数 284 浏览 1 评论 0原文

我不确定我想要实现的目标实际上是否可以实现。

我有一个可观察的集合,它的集合更改事件已经被处理。我想做的是在 observablecollection 的 collectionchanged 事件被触发之前对 observablecollection 中现有的对象列表进行一些更改。换句话说,我想在任何人从 observablecollection 中添加或删除任何对象之前对 observablecollection 中的现有对象列表执行一些操作。类似于处理集合更改事件,但不幸的是可观察集合中没有此类事件。我希望我已经说得足够清楚了。

I am not sure what i am trying to achieve is actually achievable or not.

I have an observablecollection with me and its collectionchanged event is already been handled. What i want to do is I want to make some changes in the existing list of objects in the observablecollection just before the collectionchanged event of the observablecollection gets fired. In other words i want to do something to the existing list of objects in the observablecollection before anyone adds or removes any object from the observablecollection. Something like handling the collectionchanging event but unfortunately there is not such event in observablecollection. I hope i have been clear enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

笑看君怀她人 2024-11-22 03:05:48

由于您需要在用户更改集合之前采取行动,我相信您的 CollectionChangedEvent 发生得太晚了(集合已经更改)。

相反,请考虑创建自己的从 ObservableCollection 派生的集合类,然后重写 Add()、Insert() 和 Remove() 方法以在调用基类实现之前执行其他处理。您应该能够在网络上找到相关示例。

这里有一些示例代码可以帮助您入门。它源自集合:

public class MyCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
    public MyCollection(Collection<T> list)
        : base(list)
    {
    }

    public MyCollection()
        : base()
    {
    }

    #region INotifyCollectionChanged Members

    public event NotifyCollectionChangedEventHandler CollectionChanged;

    protected void NotifyChanged(NotifyCollectionChangedEventArgs args)
    {
        NotifyCollectionChangedEventHandler handler = CollectionChanged;
        if (handler != null)
        {
            handler(this, args);
        }
    }
    #endregion

    public new void Add(T item)
    {
        // Do some additional processing here!

        base.Add(item);
        this.NotifyChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, base.Count-1));
        this.OnPropertyChanged("Count");
    }
}

Since you need to take action before the user changes the collection, I believe your CollectionChangedEvent is happening too late (the collection has already changed).

Instead, consider creating your own collection class which derives from ObservableCollection and then override the Add(), Insert(), and Remove() methods to do your additional processing before calling the base class implementation. You should be able to find examples of that on the web.

Here is some sample code to get you started. It derives from Collection:

public class MyCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
    public MyCollection(Collection<T> list)
        : base(list)
    {
    }

    public MyCollection()
        : base()
    {
    }

    #region INotifyCollectionChanged Members

    public event NotifyCollectionChangedEventHandler CollectionChanged;

    protected void NotifyChanged(NotifyCollectionChangedEventArgs args)
    {
        NotifyCollectionChangedEventHandler handler = CollectionChanged;
        if (handler != null)
        {
            handler(this, args);
        }
    }
    #endregion

    public new void Add(T item)
    {
        // Do some additional processing here!

        base.Add(item);
        this.NotifyChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, base.Count-1));
        this.OnPropertyChanged("Count");
    }
}
卖梦商人 2024-11-22 03:05:48

你已经说得很清楚了,简单的答案是:不存在这样的事件,也不可能发生。
我能想到的唯一解决方案是从 ObservableCollection 派生并自己实现该功能,即在 Add 的实现中,您首先会引发 CollectionChanging 事件,然后调用基类的 Add 方法。您将对所有其他相关方法执行相同的操作。

说了这么多,我不太确定这是正确的方法。您能提供一下您需要此功能的原因吗?

You have been clear enough and the simple answer is: There is no such event and it is not possible.
The only solution I can think of is to derive from ObservableCollection<T> and implement that functionality yourself, i.e. in your implementation of Add you would first raise the CollectionChanging event and then call the Add method of the base class. You would do the same for all other relevant methods.

Having said all that, I am not really sure, this is the correct way to do it. Can you provide a reason why you would need this functionality?

命比纸薄 2024-11-22 03:05:48

实际上,ObservableCollection 中的集合更改事件会在以下情况下触发(除其他外):

  • 将一个项目添加到 ObservableCollection 中。
  • 您从 ObservableCollection 中删除一个项目。
  • 您清除 ObservableCollection。

当我说“你”时,这意味着如果发生 CollectionChanged 事件,则意味着“你”(理解:应用程序中的某些内容)已添加、删除或清除列表。

话虽这么说,我想你只需要找到这些操作发生的位置并将你的代码放在这里......

Actually, the collection changed event in ObservableCollection is fired when (among other things) :

  • You add an item to the ObservableCollection.
  • You remove an item from the ObservableCollection.
  • You clear the ObservableCollection.

When I say "you", that means that if CollectionChanged Event occurs that means that "YOU" (understand : something in you application) has added, removed or cleared the list.

That being said, I guess you just have to find where those actions take place and put your code here...

空城旧梦 2024-11-22 03:05:48

您可以创建自己的 INotifyCollectionChanged包装集合,侦听事件,根据需要更改集合,然后发送事件。

但是当您更改集合时,会引发另一个事件,因此您必须确保正确处理这些事件,可能是通过吞咽它们

You could create your own implementation of INotifyCollectionChanged that wraps the collection, listens to the event, changes the collection as appropriate and then sends the event along.

But when you change the collection, another event is raised, so you would have to make sure you're handling those events properly, probably by swallowing them

我不是你的备胎 2024-11-22 03:05:48
public class WantDoSomethingBeforeChangeGuy
{
    internal WantDoSomethingBeforeChangeGuy()
    {
        Members = new ImplMembers(this);
    }

    public ImplMembers Members { get; }

    private class ImplMembers : ObservableCollection<Artist>
    {
        private readonly WantDoSomethingBeforeChangeGuy _owner;

        public ImplMembers(WantDoSomethingBeforeChangeGuy owner)
        {
            _owner = owner;
        }

        protected override void ClearItems()
        {
            foreach (var item in this)
            {
                item.DoSomething(_owner);
            }
            base.ClearItems();        }
    }
}
public class WantDoSomethingBeforeChangeGuy
{
    internal WantDoSomethingBeforeChangeGuy()
    {
        Members = new ImplMembers(this);
    }

    public ImplMembers Members { get; }

    private class ImplMembers : ObservableCollection<Artist>
    {
        private readonly WantDoSomethingBeforeChangeGuy _owner;

        public ImplMembers(WantDoSomethingBeforeChangeGuy owner)
        {
            _owner = owner;
        }

        protected override void ClearItems()
        {
            foreach (var item in this)
            {
                item.DoSomething(_owner);
            }
            base.ClearItems();        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文