Information
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.

How to: Set Margins of Elements and Controls

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.

Example

<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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft