Prevent connectors from being disconnected from objects

Hello,

I would like to restrict connectors to be connected only to other connectors or nodes. I looked through the constraints of the diagram and the connectors but couldn't figure out how.

Thank you.


3 Replies 1 reply marked as answer

AK Arun Kumar Sivakumar Syncfusion Team April 30, 2024 08:39 AM UTC

Hi Eshar,


Requirement: How to delete the connector being disconnected from the object.


We can achieve your requirement in our SfDiagram control, using ConnectorSourceChangedEvent and ConnectorTargetChangedEvent. We removed the connectors from the collection in those events based on the necessary conditions. The conditions are if the DragState of the connector is completed and the Source node or Target node is null. You can change the condition based on your need.  We have provided the code snippet and sample for your reference.


 

//diagram is the instance of the SfDiagram

 

(diagram.Info as IGraphInfo).ConnectorSourceChangedEvent += MainWindow_ConnectorSourceChangedEvent;

(diagram.Info as IGraphInfo).ConnectorTargetChangedEvent += MainWindow_ConnectorTargetChangedEvent;

 

 

 

 private void MainWindow_ConnectorSourceChangedEvent(object sender, ChangeEventArgs<object, ConnectorChangedEventArgs> args)

 {

     //Check whether the connector dragging is completed or not.

     if (args.NewValue.DragState == DragState.Completed)

     {

         //Check whether the connector's source node or target node is null or not

         if ((args.Item as ConnectorViewModel).SourceNode == null || (args.Item as ConnectorViewModel).TargetNode == null)

         {

             //Removed the connector from the connector collection.

             (diagram.Connectors as ConnectorCollection).Remove((ConnectorViewModel)args.Item);

         }

     }

 }

 

 private void MainWindow_ConnectorTargetChangedEvent(object sender, ChangeEventArgs<object, ConnectorChangedEventArgs> args)

 {

     //Check whether the connector dragging is completed or not.

     if (args.NewValue.DragState == DragState.Completed)

     {

         //Check whether the connector's source node or target node is null or not

         if ((args.Item as ConnectorViewModel).SourceNode == null || (args.Item as ConnectorViewModel).TargetNode == null)

         {

             (diagram.Connectors as ConnectorCollection).Remove((ConnectorViewModel)args.Item);

         }

     }

 }

 


Regards,

Arun Kumar S


Attachment: RemoveConnector_8286d844.zip

Marked as answer

EG Eshar Gal April 30, 2024 10:03 AM UTC

Thank you very much



PR Preethi Rajakandham Syncfusion Team May 2, 2024 06:23 AM UTC

Hi Eshar Gal,

You're welcome. Please let us know if you need further assistance. As always, we are happy to help you out. 

Regards,

Preethi R


Loader.
Up arrow icon