如何复制可观察集合
在这种情况下,我
Observablecollection<A> aRef = new Observablecollection<A>();
bRef = aRef();
都指向相同的 ObservableCollection。如何制作不同的副本?
I have
Observablecollection<A> aRef = new Observablecollection<A>();
bRef = aRef();
In this case both point to same ObservableCollection
. How do I make a different copy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样做:
这将创建一个可观察的集合,但项目仍然指向原始项目。如果您需要项目指向克隆而不是原始项目,则需要实现然后调用克隆方法。
更新
如果您尝试添加到列表,然后可观察集合具有原始列表,只需通过传递原始列表来创建可观察集合:
Do this:
This will create an observable collection but the items are still pointing to the original items. If you need the items to point a clone rather than the original items, you need to implement and then call a cloning method.
UPDATE
If you try to add to a list and then the observable collection have the original list, just create the Observablecollection by passing the original list:
您可以在实体定义中实现
ICloneable
接口,然后使用内部强制转换创建ObservableCollection
的副本。因此,您将拥有一个克隆的List
,而不会引用旧项目。然后,您可以使用克隆的List
创建新的ObservableCollection
实现将是
You could implement
ICloneable
interface in you entity definition and then make a copy of theObservableCollection
with a internal cast. As a result you will have a clonedList
without any reference to old items. Then you could create your newObservableCollection
whit the clonedList
The implementation would be