#282 – The Margin Property Stores a Thickness Value
April 20, 2011 Leave a comment
The Margin property, used by many controls to specify a margin around the outside of the control, is inherited from the FrameworkElement class. The Margin property’s type is Thickness, which is a structure.
The Thickness structure has four fields, defining the size of the margin along each edge of the control: Left, Top, Right, Bottom. The value of each of these fields is specified in device independent WPF Units.
You can specify a Thickness value in XAML by listing the four values in a comma-separated string, in the following order: “left, top, right, bottom”.
<Image Name="imgKlondike" Source="TractorSm.png" Margin="5,10,5,10"/>
You can also change the value of a Margin in code. You can’t set individual fields in the margin, but you can set the property to a new instance of a Thickness.
Thickness oldMargin = imgKlondike.Margin; imgKlondike.Margin = new Thickness( oldMargin.Left + 2.0, oldMargin.Top + 2.0, oldMargin.Right + 2.0, oldMargin.Bottom + 2.0);