Compartilhar via


Undo-Redo : Programmatically

One of the very essential services while authoring Workflows is Undo-Redo. Common question asked is, how do we get the service outside of VS, in a re-hosted Workflow scenario.

Well, in your re-hosted app(assuming it is WPF), it would be:

Add the following command bindings:

 <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Undo" Executed="CommandBinding_UndoExecuted" />
        <CommandBinding Command="ApplicationCommands.Redo" Executed="CommandBinding_RedoExecuted" />
</Window.CommandBindings>

Next, we need to add the handlers for these bindings, where we invoke the workflow designer services for Undo and Redo.

  private void CommandBinding_UndoExecuted(object sender, ExecutedRoutedEventArgs e)
{
   this.wd.Context.Services.GetService<UndoEngine>().Undo();
}

private void CommandBinding_RedoExecuted(object sender, ExecutedRoutedEventArgs e)
{
   this.wd.Context.Services.GetService<UndoEngine>().Redo();
}

And you are all set, you now have the Undo and Redo services as part of the re-hosted Workflow Designer Application.

One other requirement, that generally comes along with this one, is we want to have the Undo and Redo buttons added to our re-hosted app and how do we enable/disable them. In other words, how do we know that an Undo/Redo has happened. For that, we have two events: UndoUnitAdded and the RedoUnitAdded. Subscribe to these events and this should essentially tell when an Undo/redo has happened.

Event subscription:

 UndoEngine undoEngineService;

this.undoEngineService = this.wd.Context.Services.GetService<UndoEngine>();
this.undoEngineService.UndoUnitAdded += new EventHandler<UndoUnitEventArgs>(OnUndoUnitAdded);

The Event handler can be:

 void OnUndoUnitAdded(object sender, UndoUnitEventArgs e)
{
    MessageBox.Show("Undo Unit Happened");
}

Thanks,

Kushal.

Comments

  • Anonymous
    January 07, 2010
    Thanks Kushal, these little code snippets are very useful. I am a new designer. Do you have a central repository of code snippets for beginners? Thank you again! Kaplan Smith

  • Anonymous
    January 13, 2010
    @kaplan: No I do not have a central repostory. 3 blog posts which can be very useful are: http://blogs.msdn.com/mwinkle http://blogs.msdn.com/cathyk http://blogs.msdn.com/kushals 3 of us have tried to ensure we answer as many customer scenarios as possible. You can always email any of us if you have specific questions about any area. Thanks, Kushal.