0

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. There are other similar instances of this question around, but nothing that stood out as a solution.

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?

Ger
  • 169
  • 1
  • 11
  • You found "*nothing that stood out as a solution*" because your question is far too broad. As a starting point, your view model should provide all the functionality for keeping track of changes and undoing them. This is some programming work, and there no built-in support in the framework. Especially this is unrelated to the fact whether you're using an ObservableCollection or an other collection type. – Clemens Jul 29 '20 at 11:02
  • Your problem is related to how objects work in wpf. Your new list is still pointing at the same objects. If you're intending to implement undo then a fairly common approach is to serialise the entire object every time a property change is detected. This is then persisted to disk. You can therefore click undo and the previous version is deserialised, into a list used to instantiate a new observablecollection replacing the previous. This is the simple brute force approach. More sophisticated approaches are possible and quite complicated. You should explain your requirement. – Andy Jul 29 '20 at 12:44
  • I was unaware of this particular behaviour with objects in WPF. Yosef Bernal has suggested a solution that will give me what I want. That is to rollback to the last commit in a single click. I don't need change by change undo, where each click would roll back just the first change on the top of the stack, but if that turned out to be the shortest route to getting past this blocker I would accept it too. – Ger Jul 29 '20 at 13:23
  • This isn't a wpf specific feature, it's simply how reference types work. – Andy Jul 29 '20 at 13:43

1 Answers1

1

Converting an ObservableCollection<T> to List<T> simply changes the type of collection that holds your items. The elements inside the collection remain unchanged. What you're looking to do is cloning your list. To do this your element class will have to implement the ICloneable interface. See how to implement the IClonable interface here. Once that's in place, all you have to do is call:

var clone = myObservableCollection.Select(i => (MyType)i.Clone()).ToList();
Yosef Bernal
  • 1,006
  • 9
  • 20
  • There is still something off in both my understanding of how IClonable, MemberwiseClone and Clone hang together. I managed to achieve a copy using an old school copy constructor but I could not recommend my approach above Yosef's solution. – Ger Aug 04 '20 at 06:12