How to change the source and target ports of the connector at runtime in the WPF Diagram (SfDiagram)?
In the WPF Diagram, you can change the connector’s source and target ports at runtime by handling the ConnectorEditing event of the diagram. This event is triggered whenever a connector is edited or drawn, allowing you to modify its properties immediately after its creation.
Within the ConnectorEditing event, you can use the DragState property to determine the current state of the connector drag operation. This enables you to dynamically update the connector’s source and target ports by assigning the desired values to temporary variables during the event. Below is a code example demonstrating how to achieve this.
Code Snippet:
//Here diagram is the instance of the SfDiagram
(diagram.Info as IGraphInfo).ConnectorEditing += MainWindow_ConnectorEditing;
private void MainWindow_ConnectorEditing(object sender, ConnectorEditingEventArgs args)
{
if (args.DragState == DragState.Completed && args.Item is ConnectorViewModel)
{
IPort port1 = null;
IPort port2 = null;
if (MessageBox.Show("Do you want to change the connection?", "Change Connection", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
port1 = (args.Item as ConnectorViewModel).SourcePort;
port2 = (args.Item as ConnectorViewModel).TargetPort;
(args.Item as ConnectorViewModel).SourcePort = port2;
(args.Item as ConnectorViewModel).TargetPort = port1;
}
}
}
Conclusion
I hope you enjoyed learning about how to change the Connector’s source and target port at runtime in the WPF Diagram
You can refer to our WPF Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!