共用方式為


HOW TO:設定項目和控制項的邊界

更新:2007 年 11 月

本範例說明如何變更程式碼後置 (Code-Behind) 中任何現有的邊界屬性值,以便設定 Margin 屬性。Margin 屬性是 FrameworkElement 基底項目的屬性,因此各種控制項和其他項目都會繼承這個屬性。如需完整範例,請參閱設定邊界範例

這個範例是以可延伸標記語言 (XAML) 撰寫,其中包含 XAML 所參考的程式碼後置檔案。該程式碼後置同時顯示成 C# 和 Microsoft Visual Basic .NET 版本。

範例

<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);
    }
}

如需完整範例,請參閱設定邊界範例