views:

964

answers:

2

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.

+1  A: 

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

John Hunter
What about general binding, e.g. to an object derived from DependencyObject, or control-to-control binding?
Shurup
As Kent says if the object implements the IEditableObject you are quids in but if not then you need to do this manually.
John Hunter
+3  A: 

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.

HTH, Kent

Kent Boogaart
Does WPF use the IEditableObject interface, i.e. invokes its methods?
Michael Damatov
It's up to the particular control you're using. For example, the MS DataGrid control will.
Kent Boogaart