Delete Command in WPF Diagram (SfDiagram)

15 Feb 20242 minutes to read

The Delete commands are used to do delete operation on the Diagram view for currently selected item. This command is also used to delete any un-selected diagram objects with its parameter.

To execute delete commands, the DeleteParameter type has to be passed.

Delete command parameter

The DeleteParameter is used to represent the item’s parameters for executing the delete command. The DeleteParameter contains a Items property and it is used to specify a list of diagram objects that need to be deleted using the delete command.

List<IGroupable> deleteableObjects = new List<IGroupable>();

//Finding selected nodes based on IDs.
if (selectedNode != null)
{
    foreach (NodeViewModel node in diagram.Nodes as IEnumerable<object>)
    {
        if (node.ID.ToString().Equals(selectedNode.ToString()))
            deleteableObjects.Add(node);
    }
}

//Finding selected connectors based on IDs.
if (selectedConnector != null)
{
    foreach (ConnectorViewModel connector in diagram.Connectors as IEnumerable<object>)
    {
        if (connector.ID.ToString().Equals(selectedConnector.ToString()))
            deleteableObjects.Add(connector);
    }
}

if (deleteableObjects.Count > 0)
{
    //Adding deleteable objects to the DeleteParameter class using Items property.
    var parameter = new DeleteParameter() { Items = deleteableObjects };
    //Executing delete command with DeleteParameter items.
    (diagram.Info as IGraphInfo).Commands.Delete.Execute(parameter);
}
else
    //Executing delete command with null value and currently selected items will be deleted.
    (diagram.Info as IGraphInfo).Commands.Delete.Execute(null);

Delete and DeleteParameter

View sample in GitHub