How to undo changes made to an object in silverlight 3 with TwoWay binding

I have a Silverlight 3 project. I have a text box that is TwoWay data bound to an object. If the user wants to undo the changes made to the text field, what is the best way to undo changes to the associated field on the object?

I know that I could save the initial value in the separte variable when the object is loaded, but I was wondering if there is a better way to do this?

Thanks.

+5
source share
2 answers

, , . , - , INotifyPropertyChanged.

0

IEditableObject MSDN () . , , , , Microsoft, , :

public class MyObject : ViewModelBase, IEditableObject
{
   private struct MyData
   {
      string Foo,
      string Bar
   };

   private MyData Saved = new MyData()
   private MyData Current = Saved;

   public string Foo
   {
      get { return Current.Foo; }
      set
      {
         Current.Foo = value;
         OnPropertyChanged("Foo");
      }
   }

   public string Bar
   {
      get { return Current.Bar; }
      set
      {
         Current.Bar = value;
         OnPropertyChanged("Bar");
      }
   }


   public void BeginEdit() 
   { 
      if (Current == Saved)
      {
         Current = new MyData(); 
         Current.Foo = Saved.Foo;
         Current.Bar = Saved.Bar;
      }
   }

   public void CancelEdit() 
   {
      if (Current != Saved)
      { 
         Current = Saved;
         OnPropertyChanged("Foo");
         OnPropertyChanged("Bar");
      }
   }

   public void EndEdit()
   {
      if (Current != Saved)
      {
         Saved = Current;
      }
   }
}

, Current , ; IEditableObject Current.

; BeginEdit CancelEdit .

+5

All Articles