Link is created even though the IsValidLink from Relinking returns false


#1

Hi,

I have open link enabled by setting ValidUnconnectedLinks to ValidUnconnectedLinks.Allowed.
and this is working as expected but the issue is in the unconnected link if I select the link and drag connected end of the link on any where on diagram it should not create a link rather it should disallow it.

To get the above feature I have overridden the IsValidLink() and it returns false if fromnode and tonode are null. But I see that even though this method returns false the link is created without any nodes.

Any suggestions on this ?


#2

I would like to understand the scenario that you are describing. Let us say that there is a single Node and a single Link in your Diagram, and that one link is connected with the node and the other end is not connected with any node. Are you saying that when the user tries to relink the end of the link that is connected with the node, there is a new link that is created. I would expect that the RelinkingTool would disconnect that end of the link.

Maybe you can bind Route.RelinkableFrom and Route.RelinkableTo so that they are false when connected with a node?


#3

Consider the following. I have a open link connected to a node.
image

Now when I select link and try to move the connected end of the link away
image

when I do that I get this.
image

What I expect is it shouldn’t allow to reroute the link from the connected end

The following is my Code in RelinkinTool:

public override bool IsValidLink(Node fromNode, FrameworkElement fromPort, Node toNode, FrameworkElement toPort)
{
if (!base.IsValidLink(fromNode, fromPort, toNode, toPort))
return false;

        if (toNode is null || fromNode is null)
        {
            return false;
        }

}


#4

So do you want to allow relinking to another node/port, but disallow disconnecting any link entirely (i.e. not allow any Link with both FromNode and ToNode null)? While still allowing one or zero ends of links to be disconnected?


#5

Yes. I expect that when I try to disconnect the connected end of the link nothing should happen and also disallow disconnecting any link entirely.


#6

Shouldn’t that override be returning a value in case both node args are non-null?

I would write that test as toNode == null || fromNode == null, although I guess that’s OK in a recent version of C#. But using == is faster.


#7

IsValidLink() returns false but still the link is being created in diagram.


#8

Your code cannot compile successfully because it does not return a value in the case when either toNode or fromNode are not null.

Also your code would not allow the user to draw or reconnect a link so that only one end was disconnected.

Instead of overriding RelinkingTool.IsValidLink, try this:

  public class CustomRoute : Route {
    public override bool CanRelinkFrom() {
      if (this.Link.ToNode == null) return false;
      return base.CanRelinkFrom();
    }
    public override bool CanRelinkTo() {
      if (this.Link.FromNode == null) return false;
      return base.CanRelinkTo();
    }
  }

and change your Link DataTemplate(s) to use local:CustomRoute instead of a go.Route.

This causes there to be no relinking handle if the other end of the link is not connected with any node.


#9

The code I had put was just the partial code.

The solution you gave is working as expected.

Many Thanks.