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.