Ports: Add and Remove dynamically

Is it possible to add and remove ports dynamically from the code?
Can it be done through Model or does this task require manipulation with a Node visual object?

Ports are just FrameworkElements in each Node. So you can add or remove them dynamically just by modifying the visual tree of a Node.

But note that such operations do not magically modify your model. If there are any links connected to a port when you remove that FrameworkElement, you’ll either need to re-route the link so that it connects to the default port (which is normally the whole node’s VisualElement) or you’ll need to modify or delete the link data so that it refers to a different “port id”.

Normally I would think that modifying the model to effect a change in the diagram is the right way to go. That should be true both for changing the number of ports on a node as well as modifying or removing existing link data.

Thank you for your answer!
It certainly helped


Can you give an example of how to add ports dynamically in a diagram? I am new to this and struggling to figure this out. I am using the logic circuit example as a template.

I don’t have time right now to create a new example for you, but perhaps I will after I have created the next beta kits on Wednesday.

If you assume the ports could be different from each other (names, colors, shapes, etc), then your node data should have a property that is a list of data for each port. Your node DataTemplate would create a FrameworkElement for each port, bound appropriately to the corresponding port data in that list.

The EntityRelationship diagram sample demonstrates this. Each node data class (Entity) has a property (Items) that is a list of data (Attribute). The node DataTemplate uses an ItemsControl-like control that has a separate DataTemplate for each item – this is each attribute in the ER sample but would be each port in your app.

One difference is that there’s just one port per node in the ER sample. If you bind the go:Node.PortId attached property in your item template to unique values, you’ll automatically get distinct ports that your links can connect to.

There will be a new DynamicPorts sample in the v1.1 kits.