Alexander Zwitbaum
Alexander Zwitbaum

Reputation: 4846

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.

Upvotes: 7

Views: 4227

Answers (3)

Kent Boogaart
Kent Boogaart

Reputation: 178660

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.

Upvotes: 7

NathanAW
NathanAW

Reputation: 3367

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

Upvotes: 1

John Hunter
John Hunter

Reputation: 4132

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().

Upvotes: 1

Related Questions