1

My screen has a list of objects of type Person, and a form that displays the person properties (name, address, weight, etc.) via databinding.

I want to be able to:

  1. Click in a button "Edit properties", and start modifying the person's properties (I can already do that using Commands in the ViewModel);
  2. If I click "Cancel", the edited info "rollback" to the original unmodified values of that person;
  3. If I click "Save changes", JUST THEN the person's name changes in the person list.

My problem right now is that, as I am editing the forms, the original properties are updated, so to say, in realtime, and if I decide to cancel, I have not the original values to "go back".

I considered to Clone the selected person, but that seemed to be odd. I think a better approach would be to change only the text properties of the field, and update back only when clicking to submit changes, but I don't know how to do it, specially how to preserve databinding consistency.

4

3 回答 3

2

You can try it with this Undo/Redo approch

http://blog.notifychanged.com/2009/01/30/using-the-viewmodel-pattern-to-provide-undo-redo-in-wpf/

Or

You could have a a Current and Previous property created in View model to which is going to hold the edited values and the other will have original value. The required action of rollback can be performed be reassigning the values

于 2013-10-22T20:33:16.350 回答
2

When you bind, use UpdateSourceTrigger=Explicit. That will tell XAML not to update the source binding until you tell it to. Then, when your Save button is clicked, you can call UpdateSource on the binding to push the contents of your controls back to the Person object, something like:

var nameBinding = nameTextBox.GetBindingExpression( TextBox.TextProperty );
nameBinding.UpdateSource();

To cancel, use UpdateTarget instead, which will push the data in the Person object back to your control.

于 2013-10-22T20:41:22.397 回答
0

There are also open-source projects available to help with this, including http://undo.codeplex.com/

于 2013-10-22T20:43:38.097 回答