What you are looking for is a Panel
: It already exposes a Children
property of type UIElementCollection
, so all you need to do is add the children and override two methods:
MeasureOverride computes the desired size of your panel. You can return whatever size you like. To take all the available space, just return the constraint:
protected virtual Size MeasureOverride(Size availableSize)
{
return availableSize;
}
ArrangeOverride computes the location of each child as a Rect. You can easily use attached properties to store additional data for each child. This can be publicly visible data such as DockPanel.Dock or Canvas.Top, or it can be private data you use to remember where everything goes and why. The skeleton for an ArrangeOverride is:
protected virtual Size ArrangeOverride(Size finalSize)
{
foreach(UIElement child in Children)
{
Rect childLocation = ... code to compute child location ...
child.Arrange(childLocation);
}
return finalSize;
}
For drawing lines, you can either use child controls or simply override OnRender
and draw lines directly into the DrawingContext
. OnRender
is always called after ArrangeOverride
is complete and has access to the actual locations of the children.
For detailed tutorials I would Bing "WPF Panel Tutorial" or "WPF Custom Panel Tutorial". Here's one that looked good.