Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to back up the observable collection used by a WPF datagrid for the purposes of a rollback/undo button to the point of the last commit. I have a refresh option that will read from the database but that will include changes from other sources.

What I have tried:

I have tried copying the observable collection to a list, but any changes made to the OC are also persisted to that list. But aside from a single simple assignment statement that list is not in any other way connected to the observable collection.
Its almost as if the address of the observable collection was assigned to be the address of the backup list in the manner of how it is behaving. How do I overcome this?
Posted
Updated 29-Jul-20 8:20am
Comments
Garth J Lancaster 29-Jul-20 8:49am    
I keep thinking 'deep copy' / clone but I'm not sure if it is as simple as backup = ObservableCollection.Clone()
Ger Hayden 29-Jul-20 9:16am    
I will explore that thought, it might well be the solution.
[no name] 29-Jul-20 13:56pm    
You have to deep-clone every object from the source to target to maintain the source in it's original condition. There is no general "clone collection" method.

1 solution

Maybe you can use serializing, see this CodeProject article: Implementing Deep Cloning via Serializing objects[^]
As this is not a WPF article, you probably need to modify the code.
 
Share this answer
 
Comments
Ger Hayden 31-Jul-20 3:17am    
So far I have implemented both the example in Rick's solution and the deep clone from https://stackoverflow.com/questions/129389/how-do-you-do-a-deep-copy-of-an-object-in-net. Both are popping out when the memory stream statement is first run. I havent done any checking as to why yet.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900