#1,165 – Layout in Action, part II

Layout in WPF dictates how layout panels (containers) arrange their child elements.  Below is a simple example of a StackPanel containing a custom Label that we’ve instrumented so that we can see when its Measure and Arrange methods are called.  (MyPanel derives from Label).

Suppose we use the label as follows:

    <StackPanel Margin="5" Background="Honeydew">
        <loc:MyLabel Content="Billy" Background="Thistle"
                     HorizontalAlignment="Center" />
    </StackPanel>

The label looks like this at run-time:
1165-001

 

At run-time, the measure/arrange process is:

  • StackPanel calls Measure on MyLabel
  • Base Label class calculates how much space it needs (31.4 x 26) and MyLabel returns this value from MeasureOverride
  • StackPanel decides to give the label the space that it needs/wants
  • StackPanel calls Arrange on MyLabel, passing in size of 31.4 x 26
  • MyLabel.ArrangeOverride receives this value, passing on to base Label class
  • Base Label class uses this size to know how to render label

Here’s some run-time output showing these values:

1165-002

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #1,165 – Layout in Action, part II

  1. Pingback: Dew Drop – September 24, 2014 (#1862) | Morning Dew

Leave a comment