#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" />

The label looks like this at run-time:


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:



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 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 )

Connecting to %s

%d bloggers like this: