MindFusion.Wpf Pack Programmer's Guide
Undo or Redo of Property Changes

Changes to item properties are not recorded automatically for later undo. To enable undo of property changes, you must explicitly create a ChangeItemCmd instance and add it to the history queue.

The ChangeItemCmd constructor requires a reference to an item as argument and saves the item's initial state to be restored by later Undo. Call the Execute method after one or more assignments to item's properties. Execute records the final state of the item to be restored by later Redo. This allows changes to several properties of an item to be saved within a single record, which can be undone or redone as a single operation:

C#  Copy Code

// Save item state
ChangeItemCmd propChange = new ChangeItemCmd(diagram.Nodes[0], "Change");

// Change properties
ShapeNode shape = (ShapeNode)diagram.Nodes[0];
shape.Text = "new text";
shape.Brush = new SolidBrush(Color.Blue);
shape.Shape = Shape.FromId("Ellipse");

// Add to history
propChange.Execute();

Visual Basic  Copy Code

' Save item state
Dim propChange = New ChangeItemCmd(Diagram.Nodes(0), "Change")

' Change properties
Dim shape As ShapeNode = Diagram.Nodes(0)
shape.Text = "new text"
shape.Brush = New SolidBrush(Color.Blue)
shape.Shape = MindFusion.Diagramming.Shape.FromId("Ellipse")

' Add to history
propChange.Execute()

You might need to treat changes to several items as an atomic operation. To do so, create a CompositeCmd instance and add the ChangeItemCmd commands to the composite by the AddSubCmd method:

C#  Copy Code

if (diagram.Selection.Nodes.Count == 0)
    return;
 
// Make all changes seem like a single operation by
// putting them in composite Command
CompositeCmd composite = new CompositeCmd(
    diagram, "Change selection");
 
foreach (DiagramNode node in diagram.Selection.Nodes)
{
    ShapeNode shape = node as ShapeNode;
    if (shape == null)
        continue;
 
    // Save item state
    ChangeItemCmd propChange = new ChangeItemCmd(shape, "Change");
 
    // Change properties
    shape.Text = "new text";
    shape.Brush = new SolidBrush(Color.Blue);
 
    // Add to the composite
    composite.AddSubCmd(propChange);
}

// Store final state of all contained commands
composite.Execute();

Visual Basic  Copy Code

If diagram.Selection.Nodes.Count = 0 Then
    Return
End If

' Make all changes seem like a single operation by
' putting them in composite Command
Dim composite As CompositeCmd = New CompositeCmd( _
    diagram, "Change selection")

Dim node As DiagramNode
For Each node In diagram.Selection.Nodes

    Dim shape As ShapeNode = CType(node, ShapeNode)
    If Not shape Is Nothing Then

        ' Save item state
        Dim propChange As ChangeItemCmd = New ChangeItemCmd(shape, "Change")

        ' Change properties
        shape.Text = "new text"
        shape.Brush = New SolidBrush(Color.Blue)

        ' Add to the composite
        composite.AddSubCmd(propChange)

    End If

Next

' Store final state of all contained commands
composite.Execute()