views:

222

answers:

2

I have an ObervableCollection of data items. This collection is bound to a ListBox. When the user selects a item from the listbox and clicks the Edit button a UserControl with the details of that item is displayed with the various properties bound to text boxes. Each binding mode is set for TwoWay. On this details UserControl, I would like to implement 2 buttons, OK and Cancel. This would be consistent with UIs in Windows. This application is using the Model-View_ViewModel pattern. Here is my question:

1) How can I implement the Cancel button when all the changes have already been committed?

A: 

You could bind to a copy of the real collection and then only commit the changes on an OK click, or you could rework your data objects to allow transacted changes whereby you can call Commit or Rollback depending on the dialog result.

Jeff Yates
A: 

The short answer is that you can't. The original values in the data object aren't stored anywhere by SL, so unless you change your process you can't revert the changes that were made as a result of databinding.

I'd suggest making a copy of the object you want to edit before putting it in the dialog box. If your object is simple, you can easily add a "MakeCopy" member:

public MyObject MakeCopy()
{
    return (MyObject)base.MemberwiseClone();
}
Ben M