Reverting an object is a user clicks "Cancel" in WPF Reverting an object is a user clicks "Cancel" in WPF wpf wpf

Reverting an object is a user clicks "Cancel" in WPF


In object which is set to DataContext (ideally it should be ViewModel in MVVM approach) expose two commands

public ICommand CancelCommand { get; set; }public ICommand OkCommand { get; set; }

Then for the buttons assign these commands like shown below

<Button Command="{Binding CancelCommand}" ... />

You've to keep two copies of object, a copy should be created by Deep Copy or if an object has a few editable fields you can keep those as class fields. Basically on initialization stage do backup editable object properties, then bind to DataContext editable version of object. In Cancel Command handler - restore from a backup copy...


When the object is simple (just a few properties of basic types such as string, int, etc.) DeepCopy or IEditableObject is a very good option.

When the object is a node in a more complex hierarchy this might prove to be too difficult and going back to the server/model and reloading the original data is much easier.