Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo in WPF Bindings

How to provide an undo / redo using bindings in WPF?

e.g. You implement a master-detail view with bindings. After editing your changes were saved automatically using binding. Then you want to undo the changes.

Is there something ready-to-use in the binding for WPF? Does WPF provide some structures or interfaces for?

This question is not about how to implement undo/redo using stacks.

like image 578
Alexander Zwitbaum Avatar asked May 22 '09 09:05

Alexander Zwitbaum


3 Answers

Take a look at the IEditableObject interface. It allows you to take a snapshot of the object that implements it, and then roll back to that snapshot if necessary.

like image 84
Kent Boogaart Avatar answered Nov 07 '22 02:11

Kent Boogaart


What are you databinding to?

If you are databinding to a DataSet you can undo the changes by using the DataSet.RejectChanges() method provided you have not already called DataSet.AcceptChanges().

like image 24
John Hunter Avatar answered Nov 07 '22 01:11

John Hunter


You may find the Monitored Undo Framework to be useful. http://muf.codeplex.com/

It doesn't use the "top down" command pattern, but instead monitors for changes as they happen and allows you to put a delegate on the undo stack that will reverse the change.

In your case, if you're binding to an underlying model / viewmodel, then you could hook up the framework to capture these changes and then undo / redo them as needed. If the model implementes INotifyPropertyChanged and uses ObservableCollections, it should automatically reflect actions performed on the model, including undo / redo actions.

You can find more info and documentation on the codeplex site at http://muf.codeplex.com/.

like image 1
NathanAW Avatar answered Nov 07 '22 01:11

NathanAW