WPF Diagrams: Dynamic connection point creation
Tagged as WPF DiagramsIn this blog post we take a look at one of our latest additions to the WPF Diagramming product, the ability to dynamically add connection points to a node.
Say we want to design a node representing some kind of intersection in a network. The intersection has input flows and output flows. Each input and output connection should have its own connection point to make it easier to follow the diagram, rather than having all the connections bunched up on one point. To make it flexible for the user, we want to be able to have a variable number of connection points on the node. Hence we want to be able to dynamically add and remove connection points from instances of this node.
Here is how we go about implementing such a node:
1. We start by creating a new class that extends from FlowDiagramNode.
2. Include an ObservableCollection<IDiagramConnectionPoint> field to hold all the connection points. I’m calling this _connectionPoints.
3. In the constructor, we setup all the initial connection points on this node, and add them all to the observable collection as follows:
public SampleNode1() { _connectionPoints = new ObservableCollection<IDiagramConnectionPoint>(); _connectionPoints.Add(new InboundConnectionPoint(this, Edge.Top)); _connectionPoints.Add(new OutboundConnectionPoint(this, Edge.Bottom)); }
4. Now we want to override the FlowDiagramNode.ConnectionPoints property to return the observable collection.
5. As both outbound and inbound connection points are stored in the same collection, we need to implement the InboundConnectionPointsCore and OutboundConnectionPointsCore properties in a way that looks through our observable collection and picks out the appropriate points to return. One way to do this is as follows:
protected override IList<InboundConnectionPoint> InboundConnectionPointsCore { get { List<InboundConnectionPoint> result = new List<InboundConnectionPoint>(); foreach (IDiagramConnectionPoint point in _connectionPoints) { InboundConnectionPoint p = point as InboundConnectionPoint; if (p != null) { result.Add(p); } } return result; } }
6. Done!
Connection points can now be freely added or removed from the observable collection which results in dynamic connection point creation and deletion at the graphical layer. Note that the above code simply illustrates how to get dynamic connection point creation and deletion working, some more logic is required to specify when exactly the connection point collection is modified. How you add and remove connection points is up to you as it’s likely application specific. You could allow there to always be 1 unused connection point available on a node by adding or deleting connection points when connections are made or removed from the node. Or you could have a control somewhere in your application or on the node itself which the user can use to specify the number of input and output points.
Here are a couple of screenshots of one possible way dynamic connection point creation could be implemented. First we have network intersection node with one input point on the top, and one input point on the bottom as its initial state.

When the user introduces an input flow to the connection point on the top, a new input point can be added to the node as seen in the next screenshot. This will allow additional inbound connections being introduced by the user if necessary.

The WPF Diagramming products support this type of customisation because we know that nearly every application will require something different. Need to have a minimum or maximum number of connections? No problem – do whatever you require!
If you want to create some amazing diagrammatic visualizations with WPF then check out our WPF Flow Diagrams or WPF Star Diagrams products.
Leave a Reply
Categories
BrainDump (1)
Community Code (4)
Events (16)
F# (14)
General (53)
Lab Samples (2)
LightSpeed (268)
MegaPack (8)
News (71)
NHibernate Designer (26)
Nightly news (53)
Phone Elements (24)
Products (87)
Projects (5)
Screencast (6)
SharePoint (3)
Silverlight (14)
Silverlight Elements (66)
SimpleDB Management Tools (20)
Visual Studio (9)
VS File Explorer (7)
Web Workbench (39)
WPF (44)
WPF Diagrams (57)
WPF Elements (110)
WPF Property Grid (32)



Posted by Jason on 27 September 2009 


