#1,192 – Calling Arrange on Child Elements

When you author a custom control that contains child elements, you should call the Arrange method of each child object within your ArrangeOverride method.

Below is an example, from a custom element with one child.  The parent control has a dependency property, ChildProperty, representing the single child element.

        protected override Size ArrangeOverride(Size finalSize)
        {
            UIElement childElement = (UIElement)GetValue(ChildProperty);
            if (childElement != null)
                childElement.Arrange(new Rect(new Point(0.0, 0.0), finalSize));

            return finalSize;
        }

Below is a second example, from a custom panel that renders a treemap-like element.  The ChildrenTreemapOrder method returns an enumerable of ChildAndRect objects.

protected override Size ArrangeOverride(Size finalSize)
{
    foreach (ChildAndRect child in ChildrenTreemapOrder(InternalChildren.Cast<UIElement>(), finalSize))
        child.Element.Arrange(child.Rectangle);

    return finalSize;
}

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

3 Responses to #1,192 – Calling Arrange on Child Elements

  1. Pingback: Dew Drop – October 31, 2014 (#1889) | Morning Dew

  2. Kevin Donn says:

    Seems like you pasted in the same code snippet twice.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: