Problem with IsValidLink event

Hi!
I’m trying to use LinkingTool to detect the linking connections between the nodes but something goes wrong when i connect the nodes. The scenario is: 2 input nodes (node 1 and node 2) and 2 output nodes (node 3 and node 4). When i connect the input node 1 to output node 3 the IsValidLink event is fired 2 times, as i have connected the input node 1 to output node 3 and also to output node 4). What could be wrong with the IsValidLink event? Thanks for your support

My code:

public class MyCustomLinkTool : LinkingTool
{
    public override bool IsValidLink(Node <b>fromNode</b>, FrameworkElement fromPort, Node <b>toNode</b>,                                                              FrameworkElement toPort)
    {
        if (!base.IsValidLink(fromNode, fromPort, toNode, toPort)) return false;

        BaseNodeData fromNodeData = fromNode.Data as BaseNodeData;
        BaseNodeData toNodeData = toNode.Data as BaseNodeData;

        //NOTE: The inputs array begins with the index 0 
        //      The outputs array beginswith the index 500
        int fromPortIndex = Convert.ToInt32(fromNode.GetPortName(fromPort)) - 500;
        int toPortIndex = Convert.ToInt32(toNode.GetPortName(toPort));

        //Check if the connected ports are of the same type and if the INPUTS ports connects with               //the OUTPUTS ports
        if (fromNodeData.InputsIOTag[fromPortIndex].GetType() == toNodeData.OutputsIOTag[toPortIndex].GetType())
        {
            Console.WriteLine("CONNECT LinkingTool: " + " From =" + fromNodeData.Key + " To = " + toNodeData.Key + " FromPort = " + fromPortIndex + " ToPort = " + toPortIndex + " Is linked = " + base.IsValidFrom(fromNode, fromPort));
        }
        else
            return false;
        return true;
    }

Are you talking about the situation where the user is trying to draw a new link from node 1 to node 3? And if so, are you asking about why IsValidLink is being called twice, with potential “to” nodes 3 and 4?

The reason is because of link snapping to valid ports. The linking tools have a property, LinkingBaseTool.PortGravity, which controls how far away the linking tools might snap to a potential valid port. In order to implement this functionality, the linking tools have to test all of the ports that are in the area for possibly valid links.

Yes Walter, my problem is exactly what you described. It’s possible to somehow i tag the nodes that i connect to avoid that the same nodes are connected twice? I think that change the LinkingBaseTool.PortGravity property could bring problems. Do you have any suggestion? Thanks

IsValidLink is only a predicate to check if a link is possible between two ports – it does not actually create any link or modify anything.

What i need is to to know the nodes that are real connected. I would like to have an event that fires always that are a new connection is established and update my code. There are some event that fires after the connection is established to avoid this “false” connections? Thanks Walter

The Diagram.LinkDrawn and LinkRelinked events might be what you are looking for.

Or more generally, implement a Model.Changed event handler to look for ModelChange cases that you care about.

Thanks Walter. That was exactly what i needed.