Lock connector to a Node

I am looking for a way to ensure that a connector cannot be moved away from the node it is attached to. For background:

I am creating nodes and connectors dynamically and utilizing Automatic Port Creation to allow the connectors to move when the nodes move around the diagram. But I do not want a user to be able to move the connector away from the two nodes it is attached to. I tried to override the ValidateConnection method but it appears that with the Automatic Port Creation, the assignment of the nodes and the ports is not available unless the connection is moved within the node. Below is a screenshot showing that the connector and port properties of the args parameter are all null. 


How can I make sure that a connector stays with the node that it is assigned?  Note that I do want the user to be able move the connector within the node, just not away from it. 


Thank you for the assistance. 


3 Replies

DT Deepa Thiruppathy Syncfusion Team May 4, 2022 10:47 AM UTC

Hi Stephen Shake,


Requirement: How to restrict connector source and target changing from native nodes to other nodes.


We have achieved your requirement by using ConnectorTargetChangedEvent and ConnectorSourceChangedEvent. When source and target nodes are set to null from connected nodes, then nodes will be reset to respective connectors using event arguments.


Code snippet:


//Adding events.

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

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

 

//Target changed event.

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

{

    if (args.NewValue.Node == null && args.OldValue.Node != null)

    {

        NodeViewModel node = args.OldValue.Node as NodeViewModel;

        //Reset target node when it is changed to null from target node.

        (args.Item as ConnectorViewModel).TargetNode = node;

    }

}

 

//Source changed event.

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

{

    if (args.NewValue.Node == null && args.OldValue.Node != null)

    {

        NodeViewModel node = args.OldValue.Node as NodeViewModel;

        //Reset source node when it is changed to null from source node.

        (args.Item as ConnectorViewModel).SourceNode = node;

    }

}


Regards,

Deepa Thiruppathy



SS Stephen Shake replied to Deepa Thiruppathy May 4, 2022 11:26 PM UTC

This worked great! Thank you.



DT Deepa Thiruppathy Syncfusion Team May 5, 2022 06:53 AM UTC

Welcome. Please get back to us if you need any further assistance.


Loader.
Up arrow icon