Performing Undo/Redo

I’m running into some problems with undo/redo not working in certain situations, and am trying to get it working. The cases I have run into are:

1 - My diagram does undo/redo moving nodes as expected, but I have some alignment functions that move multiple nodes which is not undo-able.

2 - I have an event handler for Diagram.ExternalObjectsDropped which makes the node resizable/rotatable, but when I add a node, press undo to remove it, and then redo to re-add the node the handler (not surprisingly) doesn’t fire. Is there an event for when a node is re-added to the diagram via redo? or some other way I could implement this?

3. After drawing an unconnected line and pressing undo to remove the line, redo doesn't redraw the line. However, if I then perform another action and press undo the line appears. The unconnected line drawing tool is a custom DiagramTool I have created, so I'm not sure if there is something I need to add for it to behave properly.

  1. Does each of your alignment functions make all of its changes inside a transaction?

  2. Undo and redo only work for model state. Since you made changes to FrameworkElements instead of to node data objects in the model, the UndoManager doesn’t know about them.

I suppose you could implement a Model.Changed event handler to look for ModelChange.FinishedRedo or ModelChange.FinishedUndo. Then search the CompoundEdit of ModelChangedEventArgs for cases such as ModelChange.AddedNode and ModelChange.RemovedNode. But beware that such cases will be raised by a lot of other reasons besides undo/redo – such as when the node is dropped or when your code adds a node programmatically. But DiagramModel.IsChangingModel is true during an undo or redo.

The Update Demo sample demonstrates how to do this kind of thing, although it does so just to update the UI with information about any state changes that have just happened.

  1. The DraggableLink sample exhibits such a problem, so we’ll need to investigate.

Any updates on number 3? I’ve gotten the other issues fixed.


It’s on the list of things to investigate before we ship version 1.3.