For every one else stumbling over this:
I solved this issue now, using hierarchical ViewModels. Each command has its own ViewModel now.
public class ViewModel
{
public ViewModel()
{
MoveCommand = new MoveCommand(this);
}
public Shape Shape {get;set;}
public Point CurrentPosition {get;set;}
public ICommand MoveCommand {get; private set;}
}
public class MoveCommand
{
ViewModel viewModel;
Point shiftVector;
public MoveCommand(ViewModel viewModel, Point shiftVector)
{
this.viewModel = viewModel;
this.shiftVector = shiftVector;
}
public void Execute(object parameter)
{
shapeVM.CurrentPosition.X += shiftVector.X;
shapeVM.CurrentPosition.Y += shiftVector.Y;
}
public void Undo()
{
shapeVM.CurrentPosition.X -= shiftVector.X;
shapeVM.CurrentPosition.Y -= shiftVector.Y;
}
public bool CanExecute(object parameter)
{
}
// Notice here: the events should be passed to the command manager to take care about it
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove {CommandManager.RequerySuggested -= value;}
}
The hierarchical part comes into play as follows:
public class BaseViewModel
{
ObservableCollection<ViewModels> ViewModels;
// pass the command from the child's viewModel.
public ICommand MoveCommand
{
get
{
return SelectedItem.MoveCommand;
}
}
public SelectedItem ViewModel {get;set;}
public BaseViewModel()
{
}
}