Silverlight MVVM IEditableObject Dialog
I am playing around with the BookShelf demo application by John Papa. And would like to make some adjustments in how a book item is edited. In that application both the BookView and the EditBookWindow is bound to the same ViewModel BookViewModel which is fine.
Selecting a book will cause the EditBookWindow to be opened in a childwindow
private void OnLaunchEditBook(LaunchEditBookMessage msg)
{
var editBook = new EditBookWindow();
editBook.Show();
}
If you edit any of the values the data for the selected book will be updated in the BookViewModel. Now this is where the problem occurs. If you press Cancel on the dialogwindow the changes will still persist.
private void OKButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
private void CancelButton_Click(object 开发者_如何学运维sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
What I would like to do is to change this to "rollback" the entity to it's state before you opened the dialogwindow and started editing.
My google search on this issue leads me to think that most efficient (and easy) way of solving this is by using the IEditableObject interface : BeginEdit, EndEdit or CancelEdit.
I'm having trouble figuring out how to implement this interface. As both the EditBookWindow and the BookView is sharing the same ViewModel, the item changed is stored in the property SelectedBook
private Book _selectedBook;
public Book SelectedBook
{
get { return _selectedBook; }
set
{
_selectedBook = value;
RaisePropertyChanged("SelectedBook");
}
}
- Is the IEditableObject the most easy approach to my problem?
- Can anyone give some pointers on where (ViewModel, Views) and how I could implement the interface?
This is the same problem adressed by this post and the same solution can be applied. :-)
In short create a copy of your model, initialize your detail view with it. Alternatively hold a copy of your model inside your view model, and reset it when cancel is pressed. Or reload the item from the data source on cancel.
Edit:
If you want to use IEditableObject
you can save the model on BeginEdit
, clear the saved state on EndEdit
, and use the saved state to restore the original state on CancelEdit
. The usage of the JavaScriptSerialize
r makes creating backup copies quite easy.
Using IEditableObject
is just a variation, but not a completely different concept. With using IEditableObject
the model it self is responsible for handling the rollback mechanism. In the first approach the ViewModel
is responsible.
If you can edit the Model
using IEditableObject
is fine, but often you cannot modify the model, e.g. if you model is generated by a proxy when accessing a web service.
精彩评论