0

I am using the command pattern, and an undo action for each command. My View is bound to the commands via the viewmodel.

ExampleCode:

XAML:

<Button Command="{Binding MyCommand}">

Viewmodel:

public class ViewModel : UndoRedoClass
{
    public ViewModel()
    {
        MyCommand = new MyCommand(this);
    }

    public ICommand {get;private set;}
}

UndoRedoClass

public class UndoRedoClass
{
    private Stack<IUndoCommand> undoCommands;

    //...

    ExecuteCommand(IUndoCommand cmd)
    {
        undoCommands.push(cmd);
        cmd.Execute();
    }
}

So I can call commands generally by ViewModel.ExecuteCommand(cmd) which get pushed correctly to my undo stack. If I'd use events this is no problem, as I can use the ViewModel's ExecuteCommand method there to acutally perfom the changes. However if I offer the commands as a property, to call them from the view, they won't be added to the stack but just executed, of course.

Now the question is, where do I push the executed command on the undo stack?

loother
  • 199
  • 1
  • 8

1 Answers1

0

Derive a new ICommand type for your UndoableCommand that handles the undo/redo internally and dependency inject your undo/redo service into it. Have those UndoableCommands as properties of your VMs.

MrDosu
  • 3,427
  • 15
  • 18