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?