Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FrameworkElement.Margin Property

Gets or sets the outer margin of an element. This is a dependency property.

Namespace: System.Windows
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:

public Thickness Margin { get; set; }
/** @property */
public Thickness get_Margin ()

/** @property */
public void set_Margin (Thickness value)

public function get Margin () : Thickness

public function set Margin (value : Thickness)

    <Thickness Left="left" Top="top" Right="right" Bottom="bottom"/>
<object Margin="left,top,right,bottom"/>
- or -
<object Margin="left,top"/>
- or -
<object Margin="ThicknessReference"/>
 left, right, top, bottom  Number values between 0 and PositiveInfinity that specify the four possible dimension properties of a Thickness structure. The attribute usage will also accept abbreviated values that apply in the order provided, symmetrically and logically. For instance, Margin="20" will be interpreted to mean a Thickness with all properties set to 20. Margin="20,50" will be interpreted to mean a Thickness with Left and Right set to 20, and Top and Bottom set to 50. The default unit for a Thickness measure is . You can also specify other units by appending the unit type strings cm, in, or pt to any measure. Number values provided as XAML attributes need not specify decimal points (0 is acceptable, does not have to be provided as 0.0). For more information on Extensible Application Markup Language (XAML) usage, see Thickness.  ThicknessReference  An object reference to an existing Thickness. This might be a {Binding}}, a {StaticResource}, or {DynamicResource}} reference. For more information on Extensible Application Markup Language (XAML) usage, see Thickness. 

Property Value

Provides margin values for the element. The default value is a Thickness with all properties equal to 0 (zero).

Identifier field


Metadata properties set to true


The margin is the space between this element and other elements that will be adjacent when layout creates the user interface (UI). Shared elements might be peer elements (such as other elements in the collection of a common parent control), or might also be this element's parent.

Margin is set as a Thickness structure rather than as a number so that the margin can be set asymmetrically. The Thickness structure itself supports string type conversion so that you can specify an asymmetric Margin in XAML attribute syntax also.

A non-zero margin applies space outside the element layout's ActualWidth and ActualHeight.

Margins are additive for sibling elements in a layout; for example, two adjacent elements both with a margin of 30 set on the adjoining edge would have 60 units of space between them.

Elements that have margins set will not typically constrain the size of the specified Margin if the allotted rectangle space is not large enough for the margin plus the element content area. The element content area will be constrained instead when layout is calculated. The only case where margins would be constrained also is if the content is already constrained all the way to zero.

This example describes how to set the Margin property, by changing any existing property value for the margin in code-behind. The Margin property is a property of the FrameworkElement base element, and is thus inherited by a variety of controls and other elements. For the complete sample, see Setting Margins Sample.

This example is written in Extensible Application Markup Language (XAML), with a code-behind file that the XAML refers to. The code-behind is shown in both a C# and a Microsoft Visual Basic .NET version.

<Button Click="OnClick" Margin="10" Name="btn1">
Click To See Change!!</Button>

void OnClick(object sender, RoutedEventArgs e)
    // Get the current value of the property.
    Thickness marginThickness = btn1.Margin;
    // If the current leftlength value of margin is set to 10 then change it to a new value.
    // Otherwise change it back to 10.
    if(marginThickness.Left == 10)
         btn1.Margin = new Thickness(60);
    } else {
         btn1.Margin = new Thickness(10);

For the complete sample, see Setting Margins Sample.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
© 2018 Microsoft