#1,169 – Custom Panel, part I (Measure and Arrange)
September 30, 2014 1 Comment
You can create a class that derives from Panel in order to create a panel control with custom behavior. You typically override the following two methods in your custom panel:
- MeasureOverride – You call Measure method on each child element to determine how much space they need, then return the total space needed
- ArrangeOverride – You call Arrange method for each child element to position them
Below is a very simple example–a panel that does nothing in either its MeasureOverride or ArrangeOverride methods. We’ll build on this class in future posts.
public class MyPanel : Panel { protected override Size MeasureOverride(Size availableSize) { Size measuredSize = base.MeasureOverride(availableSize); return measuredSize; } protected override Size ArrangeOverride(Size finalSize) { Size arrangedSize = base.ArrangeOverride(finalSize); return arrangedSize; } }
If we add children to this panel, they will not be displayed. Below is debug output at run-time. MeasureOverride returns (0,0) because we haven’t calculated any sizes.