Articles in this section
Category / Section

How to update the same length for connector segments in the WPF Diagram (SfDiagram)?

3 mins read

WPF Diagram supports updating the same length for the connector’s first and last segments. When drawing an orthogonal connector between two nodes in our SfDiagram, by default, the length for the initial segment is set to 20 pixels.

The same length for the connector’s first and last segments is achieved by using the LoadSegments() and ToPoints() methods of a connector, where the ToPoints() method is to get the list of segments points of a connector and LoadSegments() method is to update the connector segment based on a list of segments points passed in this method. We have retrieved the segment points of a connector from the ToPoints() method and modified those points using the LoadSegments() method.

We have created a sample to update the average length for the connector segments of all the connectors connected between two nodes. Please refer to the following code example.

C#

//Getting segment points of the connector drawn.
List<Point> segmentsPoints = (connector.Info as IConnectorInfo).ToPoints(true).ToList();

//Total segment count of the connector.
int segmentsCount = segmentsPoints.Count;

//For non-edited segments.
if (segmentsCount == 4)
{
    List<Point> newPoints = new List<Point>();

    //For top and bottom direction connectors.
    if (segmentsPoints[0].X == segmentsPoints[1].X)
    {

        //Average y distance needs to be updated for the segment.
        double average = (segmentsPoints[segmentsCount - 1].Y - segmentsPoints[0].Y) / 2;

        //New y position for intermediate segments.
        double newy = segmentsPoints[segmentsCount - 1].Y - average;

        //Updating new y point to the intermediate segments.
        for (int i = 1; i <= segmentsCount - 2; i++)
        {
            Point pnt = new Point(segmentsPoints[i].X, newy);
            newPoints.Add(pnt);
        }
    }

    //For left and right direction connectors.
    else if (segmentsPoints[0].Y == segmentsPoints[1].Y)
    {

        //Average x distance needs to be updated for the segment.
        double average = (segmentsPoints[segmentsCount - 1].X - segmentsPoints[0].X) / 2;

        //New x position for intermediate segments.
        double newx = segmentsPoints[segmentsCount - 1].X - average;
        //Updating new x point to the intermediate segments.
        for (int i = 1; i <= segmentsCount - 2; i++)
        {
            Point pnt = new Point(newx, segmentsPoints[i].Y);
            newPoints.Add(pnt);
        }
    }


    //Passing new segment points to the connector using the LoadSegment method.
    (connector.Info as IConnectorInfo).LoadSegments(newPoints);

ezgifcom-video-to-gif-converter.gif

View sample in GitHub

Conclusion
I hope you enjoyed learning how to update the same length for connector segments in the WPF Diagram (SfDiagram).

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, check out our components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.

Please let us know in the following comments section if you have any queries or require clarifications. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied