Linking to nearest port

The node template we use has 4 ports (left,top,right,bottom).
All the ports are having linkable to and linkable from attributes set for them.The issue is when user move the node in the diagram panel.
How to always find the nearest port(or distance between the nodes and relink the objects while the objects are moved around the canvas.
This behaviour seems to be hapenning when i consider entire node to be a single spot.Since we have defined ports now.how to achieve this behavoiur?

The reason to have separate ports is because you really want to have links logically and physically connected at different point on the node. The prototypical example of this is with the nodes in an arithmetic circuit – we have to distinguish the inputs to a “Subtract” node.

If you just want to have the links connect at the closest point or side, then you want what you have discovered: have a single port which is the whole node. This is actually the default behavior in GoXam.

Having a single port per node is simpler of course, because each link doesn’t need to know the port identification for each end. If you want to convert your app to just using a single port per node, remove all of the go:Node.PortId attached properties identifying the ports. Then you can remove all uses of the port identifier (a.k.a parameter).

What we are trying to achieve having four ports is as follows:

We have 4 connectors(ports) in the node template.
All these connectors need to appear on mouse over of the node.
User should be able to drag a link from any of the port to any port.Once user connected the nodes we need to change the style of the port(from and to) that is connected and make it visible, all other ports invisible.
To achieve this i think i need to have ports at 4 quadrants.
I have another requirement that says , however when i move the node on the diagram panel i need to identify the nearest connector/port and change the link to that port irrespective of the port that user chose to connect to.
Please guide me and inform me if this can be done??
Any work around or alternative approach?
I took some code from flowchart sample to achieve this.though i have a problem selecting the nearest port.

Yes, that’s possible. In a Diagram.SelectionMoved or .SelectionCopied event handler, for all of the links that are connected to moved nodes, you would have to modify each link data’s FromPort or ToPort properties.

thanks ,

i would try that and let you know if any issues.

Something like:

foreach (Part part in myDiagram.SelectedParts) {
Node node = part as Node;
if (node == null) continue;
foreach (FrameworkElement port in node.Ports) {
foreach (Link link in node.FindLinksIntoPort(Node.GetPortId(port))) {
var linkdata = link.Data as …; // your link data Type
linkdata.ToPort = …; // find desired port id
}
foreach (Link link in node.FindLinksOutOfPort(Node.GetPortId(port))) {
var linkdata = link.Data as …; // your link data Type
linkdata.FromPort = …; // find desired port id
}
}
}