Class GridLayout
The GridLayout is a special Panel that can be used exactly like the Grid Panel, except that it defines a new property ChildMargin. ChildMargin's left, top, right and bottom margins will be applied to all children in a way that the children will have a vertical space of ChildMargin.Top+ChildMargin.Bottom and a horizontal space of ChildMargin.Left+ChildMargin.Right between them. However, there is no margin for the borders of the internal widget, so that the GridLayout itself can be aligned to another element without a margin. It's best to have a look at TestWindow, which effectively tests all possible alignments of children.
public class GridLayout : Grid, IAnimatable, IFrameworkInputElement, IInputElement, ISupportInitialize, IQueryAmbient, IAddChild
- Inheritance
-
GridLayout
- Implements
- Inherited Members
Remarks
Taken from http://daniel-albuschat.blogspot.com/2011/07/gridlayout-for-wpf-escape-margin-hell.html in 2015.
Constructors
GridLayout()
public GridLayout()
Properties
ChildMargin
Gets or sets the thickness of the child margin.
public Thickness ChildMargin { get; set; }
Property Value
Remarks
The child margin defines a margin that will be automatically applied to all children of this Grid. However, the children at the edges will have the respective margins remove. E.g. the leftmost children will have a Margin.Left of 0 and the children in the first row will have a Margin.Top of 0. The margins that are not set to 0 are set to half the ChildMargin's value, since it's neighbour will also apply it, effectively doubling it.
Methods
MeasureOverride(Size)
We change all children's margins in MeasureOverride, since this is called right before the layouting takes place. I was first skeptical to do this here, because I thought changing the margin will trigger a LayoutUpdate, which in turn would lead to an endless recursion, but apparantly WPF takes care of this.
protected override Size MeasureOverride(Size constraint)
Parameters
constraintSizeSizing constraint.
Returns
- Size
Size to apply.
UpdateChildMargins()
UpdateChildMargin first finds out what's the rightmost column and bottom row and then applies the correct margins to all children.
public void UpdateChildMargins()