While trying to implement a tool after the Dynamic Ports sample, I have issues with the Routes:
Links are made from node to node, but not from port to port…
For: Node.GetPortId(port) I get null, because port is always a Grid and not an Rectangle (-> var toport = this.Link.ToPort; and var fromport = this.Link.FromPort; are Grids)
My first reaction is that you need to check that your GraphLinksModel’s link data includes both the “FromPort” and the “ToPort” identifiers in addition to the “From” and “To” node identifiers.
If you are using GraphLinksModelLinkData<NodeKey, PortKey>, then the names of those properties happen to be “FromPort” and “ToPort”.
Your node template appears to have ports be Rectangles, because it is those elements that have the go:Node.PortId Binding. So Link.FromPort and Link.ToPort ought to be Rectangles.
If your node DataTemplate is a Grid, then not identifying and finding the specific port element on a given node would explain why Link.ToPort is returning the whole Node rather than a specific Rectangle.
public class DiagramLinkItem : GraphLinksModelLinkData<String, String>, IEquatable<DiagramLinkItem>
{
public string TypeString { get; set; }
public object Tag { get; set; }
public DiagramLinkItem(string _from, string _to, object _tag, string _fromPort, string _toPort)
{
Type type = _tag.GetType();
From = _from;
To = _to;
TypeString = type.ToString().Substring(type.GetType().ToString().IndexOf("_") + 1);
Tag = _tag;
FromPort = _fromPort;
ToPort = _toPort;
}`
I also checked if From, To, FromPort and ToPort is set with the debugger. Everything seems to be fine untill it comes to Link.FromPort and Link.ToPort.
And have you checked that the expected port identifiers are actually present on the expected Node? You can call Node.FindPort to see if it actually finds the Rectangle element that you expect.
Node.GetPortId(port) returns always null, because the port is a Grid, and the Ports are in the Grid’s InertListBoxes…
So when I try for example Node.GetPortId(((System.Windows.Controls.Grid)portTest).Children[2])) (Giving to GetPortId an InertListBox containing a port), it again returns null, even if there is a port in the Itemssource…
Yet your ItemTemplate sets the Node.PortId attached property on the Rectangle. Is that true for all your ItemTemplates? And there is no other use of Node.PortId?
yes, it is set this way for all ItemTemplates, and is never used anywhere else.
And: I have this pattern in my NodeTemplate and in the GroupTemplate again (because my groups can have ports, too).