Compartir a través de


Cómo: Definir márgenes de elementos y controles

En este ejemplo se describe cómo establecer la propiedad Margin, cambiando cualquier valor de la propiedad existente para el margen en el código subyacente. La propiedad Margin es una propiedad del elemento base FrameworkElement, por lo que la heredan diversos controles y otros elementos.

Este ejemplo está escrito en Extensible Application Markup Language (XAML), con un archivo de código subyacente al que hace referencia el marcado XAML. El subyacente se muestra en versión C# y en versión Microsoft Visual Basic.

Ejemplo

<Button Click="OnClick" Margin="10" Name="btn1">
Click To See Change!!</Button>
Private Sub OnClick(ByVal sender As Object, ByVal e As RoutedEventArgs)

    ' Get the current value of the property.
    Dim marginThickness As 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 Then
        btn1.Margin = New Thickness(60)
    Else
        btn1.Margin = New Thickness(10)
    End If
End Sub
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);
    }
}