#282 – The Margin Property Stores a Thickness Value

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);

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: