Procedura: impostare i margini di elementi e controlli
In questo esempio viene illustrato come impostare la proprietà Margin mediante la modifica di un qualsiasi valore della proprietà esistente per il margine nel code-behind. Margin è una proprietà dell'elemento di base FrameworkElement; pertanto viene ereditata da una serie di controlli e altri elementi.
Questo esempio è scritto in Extensible Application Markup Language (XAML), con un file code-behind al quale XAML fa riferimento. Il code-behind viene mostrato in una versione C# e in una versione Microsoft Visual Basic.
Esempio
<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);
}
}