Undo and Clipboard

Enabling Undo Support

Undo and redo are provided by the graph out-of-the-box, but have to be enabled before they can be used. The easiest way to do this is to enable them on both the default graph and the input mode.

To enable undo on the default graph, retrieve it and and set its UndoEngineEnabled property to true.

Example 3.23. Enabling Undo

IGraph graph = GetMyGraph();

// Retrieve the default graph and enable the undo feature
graph.Get<DefaultGraph>().UndoEngineEnabled = true;

To trigger undo and redo operations, use the convenience methods Undo and Redo or the commands Undo and Redo .

Example 3.24. Use Undo

// trigger an undo action

// trigger a redo action

Enabling Clipboard Support

By default, clipboard support is enabled on the graph. However, to use it with the default keyboard shortcuts, you must also enable it on the input mode. Once you've enabled it, the application commands for cut, copy, and paste will work and can be triggered by the standard keyboard commands Ctrl-X, Ctrl-C and Ctrl-V.

To enable clipboard support, set the input mode's ClipboardOperationsAllowed property to true.

Example 3.25. Enabling Clipboard

GraphEditorInputMode inputMode = GetMyGraphEditorInputMode();

// Enable clipboard operations on the input mode
inputMode.ClipboardOperationsAllowed = true;

To trigger clipboard operations, use the convenience methods Cut, Copy, and Paste, or the commands Cut , Copy , and Paste .

Example 3.26. Using Clipboard Operations


Customizing Undo and Clipboard Operations

It is possible to customize undo and clipboard operations. For example, if you copy a collection of nodes and edges, paste might only duplicate the nodes, or it could apply a new style to the elements.

Adding New Operation Types

The undo engine supports the basic graph operations such as adding, moving, and removing elements; changing styles, etc. However, your application may have additional custom operations, for example operations on business data stored in an element's tag. To be able to undo and redo a custom operation, you must make a custom undo unit so that the operation can be added to the undo queue.

Adding new operation types is an advanced topic that is covered in the yFiles for Silverlight tutorial, see especially step 07.