0

In my grid, upon a click, I'm providing the entire row's item to another window (modal dialog) and make it its data context. When the user is done with it, the update to the DB is made only if the dialog's result is true.

private void DataGridRow_OnClick(Object sender, MouseButtonEventArgs eventArgs)
{
  Editor dialog = new Editor((sender as DataGridRow).Item as Thing);

  if (dialog.DialogResult ?? false)
    (DataContext as ViewModel).Update();
}

This works like a charm and only updates the DB if the user clicks the accepting button. However, it also has a certain unexpected behavior. The changes made in the editor window, although it's cancelled (rendering the value of DialogResult to be false, hence omitting the update of the DB), still remain visible in the grid.

It's kind of obvious once one thinks about it because we're binding to an object (call by reference) while feeding the editee object to the constructor of the modal window. But it's hardly anything that meets the users' expectations.

I'm about to code in a workaround creating a copy of the originally sent in object and then restoring it, if the update to the DB isn't carried out.

private void DataGridRow_OnClick(Object sender, MouseButtonEventArgs eventArgs)
{
  Thing thing = (sender as DataGridRow).Item as Thing;
  Thing clone = thing.GetClone();
  Editor dialog = new Editor(thing);

  if (dialog.DialogResult ?? false)
    (DataContext as ViewModel).Update();
  else
    (sender as DataGridRow).Item = clone;
}

However, it's kind of inconvenient to keep extension methods for creating a copy (IClonable is deprecated) and all the shuffling back and forth smells a little. Is there a neater approach to resolve it?

The only other approach I can think of (and we're talking a lazy man's solution here) is to simply recreate the original data context, hence refreshing everything from the DB like so.

private void DataGridRow_OnClick(Object sender, MouseButtonEventArgs eventArgs)
{
  Editor dialog = new Editor((sender as DataGridRow).Item as Thing);

  if (dialog.DialogResult ?? false)
    (DataContext as ViewModel).Update();
  else
    DataContext = new ViewModel();
}

Thoughts?

Community
  • 1
  • 1
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • Cant you use Reflection and use that to store copy or Original Values ? Have your objects deive from this class that stores the original value of each class. We do this to avoid saving data that has not changed. http://stackoverflow.com/questions/8133974/how-to-get-original-values-of-an-entity-in-entity-framework has the same idea for use but on the server side which you may be doing on the client side. – Ace McCloud Aug 06 '15 at 22:50

1 Answers1

0

I have seen many different solutions for this from expert tutorials.

One is to copy the original values into a basic struct, which you can use to revert to by copying values from.

One is to copy the values from the model POCO object into ViewModel properties and only apply the changes when confirmed.

There are many ways to achieve the same result. Its just a matter of preference.

Glen Thomas
  • 10,190
  • 5
  • 33
  • 65