#1,193 – MeasureOverride and Margins

During the measure phase, the MeasureOverride method is called on an element, indicating the size available to the control.  If a Margin has been set on the control, the available size passed in to MeasureOverride will have already been adjusted for that margin.

Below, we include two instances of MyElement in a Grid, setting a margin of 15 on the second element.


        <loc:MyElement Grid.Column="1" Margin="15"/>

At run-time, we see that the second element is smaller.
If we instrument MyElement to report the value of the Size parameter that is passed to it, we see that MeasureOverride gets a smaller size passed in for the second instance.  The Size has been adjusted for a uniform margin of 15, subtracting 30 from both the width and the height.  (We see the same values passed in to ArrangeOverride).




#1,189 – MeasureOverride Input and Output

The MeasureOverride method in FrameworkElement takes as input a Size and then returns a Size.  These parameters are intended to be used as follows:

  • Input Size – during layout, this is how much room is available for the element to be rendered
  • Output Size – this is how much room the element says that it needs

For example, below we have a custom element that draws a big “X” using all of the available space.  (When rendering, it uses ActualWidth and ActualHeight).  Because it will use as much room as it is given, MeasureOverrides return value is all of the available space.

    public class MyElement : FrameworkElement
        protected override Size MeasureOverride(Size availableSize)
            Size sizeIWillNeed = availableSize;

            return sizeIWillNeed;

        protected override void OnRender(DrawingContext dc)
            dc.DrawLine(new Pen(Brushes.Blue, 2.0),
                new Point(0.0, 0.0),
                new Point(ActualWidth, ActualHeight));
            dc.DrawLine(new Pen(Brushes.Green, 2.0),
                new Point(ActualWidth, 0.0),
                new Point(0.0, ActualHeight));