Command pattern

I read some articles on implementing such a feature by extending the Command
pattern.

Now, WPF has an implementation of this pattern (System.Windows.Input) and
I'm using it already.

So here's my question: how do I add the Undo/Redo in an orderly way

One of the things I have considered is extending the CommandManager but:
1. how do I get my window to use it
2. it is sealed...

Any suggestions

--
Erno
----
WPF tutorials: http://blogs.infosupport.com/ernow/articles/1878.aspx





Answer this question

Command pattern

  • CWinKY

    I am not sure if this is what you want:
    If you have a custom control to handle Undo/Redo commands, you have to register the command bindings.


    static MyCustomControl()
    {
    CommandManager.RegisterClassCommandBinding(typeof(MyCustomControl), new CommandBinding(ApplicationCommands.Undo, UndoCommand));
    }


    private static void UndoCommand(object target, ExecutedRoutedEventArgs e)
    {
    MyCustomControl myControl = target as MyCustomControl;
    ...
    }


  • Scot5691

    I think I found a solution. You can download it from my blog:

    http://blogs.infosupport.com/ernow/archive/2006/02/27/3961.aspx

    Let me know what you think.

    Regards


  • Paul Dimitrov

    This is not what I am looking for. But your post brought me back on trail!

    I know how to how to setup commands and create my own.

    My problem is building a Undo/Redo stack without repeating the same code in my commandhandlers.

    Here's what I would like:

    class CustomCommandBindingCollection : CommandBindingCollection
    {
      private Stack<UndoRecord> _undoStack = new Stack<UndoRecord>();
      public CustomCommandBinding()
      {
        this.Executed += ....(this.CommandExecuted);
      }

      void CommandExecuted(object source, ExecutedRoutedEventArgs e)
      {
        switch(e.command)
        {
          select:
          _undoStack.Push(new DeselectRecord(......));
        }
    }

    You see, in the CommandPattern there is something called a CommandManager (NOT the CommandManager from System.Windows.Input as your post made me realize) all calls execute via that manager so that is the perfect place to build the Undo/Redo stack.

    Perhaps Microsoft didn't implement the Command pattern afterall... I hope they thought of a way of how to implement is functionality anyway...

    Cheers


  • amcclendon

    If you don't have a custom control but you want to handle the commands at page/window level
    you can do:
    <Page.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Undo" Executed="UndoCommand"/>
    </Page.CommandBindings>

    and in code (note that in this case the handle doesn't need to be static)
    private void UndoCommand(object target, ExecutedRoutedEventArgs e)
    {
    ...
    }



  • Command pattern