For each node you can define one or more points to which links should dock when connecting to that node. Such points are represented by instances of the AnchorPoint class. The position of an anchor point relative to node's bounding rectangle is specified via the X and Y properties. By default, their values are expressed as percent of the node's width and height to accommodate for moving and resizing. The final location of an anchor point is determined by this simple calculation:
C# Copy Code |
---|
return new PointF( |
Visual Basic Copy Code |
---|
Return New PointF( _ |
A point's position can also be defined as a fixed offset from the node's top-left corner, letting you define anchor positions that do not depend on the node's width or height. This is controlled independently for X and Y coordinates, by means of the XUnit and YUnit properties.
You can control whether an anchor point accepts incoming or outgoing links or both. That is done via the AllowIncoming and AllowOutgoing properties. When a link is drawn, its ends are aligned to the nearest anchors allowing docking of the respective end type. Another way to control whether links can be connected to an anchor point is to handle the ValidateAnchorPoint event.
An AnchorPattern instance defines a set of anchor points to be used together. When creating an anchor pattern, pass to its constructor an array of AnchorPoint objects. The AnchorPattern class exposes several predefined patterns as static properties. An existing pattern can be assigned as a single entity to the AnchorPattern property of nodes. The following example creates a pattern of four anchor points:
C# Copy Code |
---|
apat1 = new AnchorPattern(new AnchorPoint[] { |
Visual Basic Copy Code |
---|
apat1 = new AnchorPattern(new AnchorPoint() { _ |
The points are located at the middles of bounding rectangle sides of nodes to which the pattern is assigned. All four points accept both incoming and outgoing links.
A distinct anchor pattern can be assigned to every row of a table. When a link is connected to a row, the respective end of the link is aligned to the nearest AnchorPoint assigned to that row. If a row does not have associated anchor points of the appropriate type (incoming/outgoing), the link will be connected to the nearest row that has such anchors defined. If none of the rows of a table has an associated anchor pattern, links are connected to the middle of the left or right side of the pointed row.
By default, a link is attached to an anchor point only when the link is created. Afterwards, the user can move the link end to any part of the node. To ensure that links always stay attached to an anchor point even after subsequent modifications done by the user, set the SnapToAnchor property to CreateOrModify. You can prevent the user from attaching specific links to specific anchor points by handling the ValidateAnchorPoint event.
You can connect a link to a specific anchor point by setting the link's OriginAnchor and DestinationAnchor properties to the index of the point in the respective node's AnchorPattern. The ReassignAnchorPoints method of DiagramLink automatically chooses anchor points of the origin and destination nodes that are close to each other, so that the link takes a shorter path between the nodes. The ReassignAnchorPoints method of DiagramNode sets the anchor point of each link connected to the node so that links are evenly distributed among the available anchor points.