0

I have a problem. I have two observable collections with the same objects as content.

I grep one object from my datagrid with observablecollection.

datagrid.selecteditem as object

and want to delete it from the second observable collection. The line statement looks like

obscollection.remove(datagrid.selecteditem as object);

The objects are completly the same, but when I count obscollection the object isn't removed...

How can I solve this? Please no questions on why I need 2 collections with the same content ;)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

When you say "the objects are completely the same", do you mean they are equivalent, or they are the same instance? My guess is that they are equivalent, but they are not actually the same object instance. The .remove() method of the ObservableCollection will be looking for reference equality, and therefore if it is not the same object instance, it won't find the object you're looking for (and therefore won't remove it).

I'd recommend looking at information about object equivalency, this article speaks to it, as does this answer (and there are many more out there if you do a search).

If the two collections have equivalent objects, but do not reference the same instance, then there are many simple solutions. One way would be to implement the IComparable interface, or use something like LINQ to find the equivalent object in the second collection and manually remove it.

Hope this helps.

Community
  • 1
  • 1
Brian S
  • 5,675
  • 1
  • 22
  • 22