在 wpf 中的 observablecollection 中的集合更改之前做一些事情
我不确定我想要实现的目标实际上是否可以实现。
我有一个可观察的集合,它的集合更改事件已经被处理。我想做的是在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于您需要在用户更改集合之前采取行动,我相信您的 CollectionChangedEvent 发生得太晚了(集合已经更改)。
相反,请考虑创建自己的从 ObservableCollection 派生的集合类,然后重写 Add()、Insert() 和 Remove() 方法以在调用基类实现之前执行其他处理。您应该能够在网络上找到相关示例。
这里有一些示例代码可以帮助您入门。它源自集合:
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:
你已经说得很清楚了,简单的答案是:不存在这样的事件,也不可能发生。
我能想到的唯一解决方案是从
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 ofAdd
you would first raise theCollectionChanging
event and then call theAdd
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?
实际上,ObservableCollection 中的集合更改事件会在以下情况下触发(除其他外):
当我说“你”时,这意味着如果发生 CollectionChanged 事件,则意味着“你”(理解:应用程序中的某些内容)已添加、删除或清除列表。
话虽这么说,我想你只需要找到这些操作发生的位置并将你的代码放在这里......
Actually, the collection changed event in ObservableCollection is fired when (among other things) :
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...
您可以创建自己的
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