New to Telerik UI for WPF? Download free 30-day trial

Removing Connected Connections on Item Deletion in RadDiagram

Environment

Product RadDiagram for WPF
Version 2023.3.1114

Description

How to remove attached RadDiagramConnections when the associated diagram shape is deleted.

Solution

To do this, you can use the RadDiagram's ItemsChanging event. In the event handler, you can access the deleted shape and remove its connections from the diagram. To access the connections attached to the shape, you can use the IncomingLinks and OutgoingLinks properties of the shape.

private void RadDiagram_ItemsChanging(object sender, Telerik.Windows.Controls.Diagrams.DiagramItemsChangingEventArgs e) 
{ 
    if (e.OldItems.Count() > 0) 
    { 
        var diagram = (RadDiagram)sender; 
        var shapes = e.OldItems.OfType<RadDiagramShapeBase>(); 
        foreach (var shape in shapes) 
        { 
            foreach (var connection in shape.IncomingLinks.ToList()) 
            { 
                diagram.RemoveConnection(connection, true); 
            } 
 
            foreach (var connection in shape.OutgoingLinks.ToList()) 
            { 
                diagram.RemoveConnection(connection, true); 
            } 
        } 
    } 
} 
In this article