I've just read this question: How to cancel an edit to an object using MVVM?
I have the exact same question and would like to have a simple solution. The first one looked very promising, however, I'm using entity framework and my classes are automatically generated, so that's not an option.
How can I do this easily with EF?
EDIT: My ViewModel:
public List<Player> Players
{
get { return repository.Players.OrderBy(x => x.Firstname).ToList(); }
}
public Player CurrentPlayer
{
get { return currentPlayer; }
set
{
if (currentPlayer != value)
{
currentPlayer = value;
RaisePropertyChanged("CurrentPlayer");
}
}
}
Players is bound to a datagrid, CurrentPlayer to the selecteditem of that. Below the datagrid, I have textboxes where the user can edit the player info.
When the user presses the save button, this code is executed:
private void SaveExecute(object parameter)
{
repository.SavePlayer(currentPlayer);
Editing = false;
}
Very easy. When the user presses the cancel button, this is executed:
private void CancelExecute(object parameter)
{
if (currentPlayer.Id == 0) // id = 0 when a new player is being added
{
CurrentPlayer = null;
}
else
{
// here, the CurrentPlayer should be set back to it's previous state.
}
Editing = false;
}
CurrentPlayer is an object of Player, which is an entity class generated by EF.