Authoring layout panels
Implement custom measurement and arrangement when the built-in layouts are not enough.
Derive from Panel for a layout with placement rules that cannot be expressed clearly with Grid, StackPanel, or Overlay. A panel participates in two passes:
MeasureOverrideoffers space to each child and returns the panel's desired size.ArrangeOverrideassigns every child a final rectangle.
The shared measurement vocabulary is covered in sizing and safe areas.
This panel keeps two alternative children measured and arranged in the same bounds while selecting which size controls the parent. It is the pattern used by the Gallery's preview/code switcher:
DesiredSize includes a child's margin. Arrange applies that margin and the child's alignment inside the rectangle you provide, so panels should pass the full slot rather than subtracting the margin a second time.
Call InvalidateMeasure whenever a property changes the desired size or child placement. Purely visual changes such as opacity or color do not need another measure pass.
Layout rules
Measure children with finite constraints where the layout has a real bound. An infinite dimension means the child may request its natural size; handing infinity through every layer can make scroll content or wrapping ambiguous.
Return a finite desired size unless the parent explicitly supports an open dimension. During arrangement, position every child that was measured, including inactive children that must remain ready for an animated transition. Set IsVisible false when a child should leave layout entirely.