如何:设置元素和控件的边距
此示例介绍如何通过更改代码隐藏中边距的任何现有属性值来设置 Margin 属性。 Margin 属性是 FrameworkElement 基元素的属性,因此由各种控件和其他元素继承。
此示例是使用 Extensible Application Markup Language (XAML) 编写的,其中包含 XAML 引用的代码隐藏文件。 代码隐藏同时以 C# 和 Microsoft Visual Basic 版本显示。
示例
<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);
}
}
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