I'm implementing a feature where multiple ContainerNode objects can be placed inside one another to create a layered swim lane.
One part of this functionality is changing the size of the parent ContainerNode to fit the child ContainerNodes. However, something strange happens. When a second ContainerNode is added to the parent, the parent moves. The image which I have attached to this post contains an example. A parent ContainerNode is placed on the corner of another node and then a second child is added to the parent. This moves the parent, while the children stay in the right spot.
The code that is causing the issue is the following:
public void ChildAdded()
{
if (!this.OldBounds.IsEmpty)
{
float totalHeight = 0.0f;
foreach (DiagramNode connectedNode in this.SubordinateGroup.AttachedNodes)
{
MyContainerNode connectedContainer = connectedNode as MyContainerNode;
if (null != connectedContainer)
{
totalHeight += connectedContainer.Bounds.Width;
}
}
PointF location = new PointF(this.OldBounds.X, this.OldBounds.Y);
SizeF size = new SizeF(totalHeight, this.OldBounds.Height);
RectangleF bounds = new RectangleF(location, size);
this.SetBounds(bounds, true, true);
}
}
This method is a part of the MyContainerNode class and the OldBounds property is a copy of the Bounds of the ContainerNode before the child was added.
Note: the MyContainerNode objects are rotated 270 degrees, which is why 'totalHeight' actually containers the width of the children.
Using the code above, the Bounds.Location of the parent does
not change when the second child is added.
If I remove the call 'this.SetBounds(bounds, true, true);' the parent will not move.
What could be causing the parent lane to move?