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?