#1,165 – Layout in Action, part II
September 24, 2014 1 Comment
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:
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: