Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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/.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top