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.
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.
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().
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/.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With