次の方法で共有


方法 : 要素およびコントロールのマージンを設定する

更新 : 2007 年 11 月

この例では、分離コードでマージンに関する既存のプロパティ値を変更することで、Margin プロパティを設定する方法を説明します。Margin プロパティは FrameworkElement 基本要素のプロパティであるため、各種コントロールやその他の要素によって継承されます。サンプル全体については、「マージンの設定のサンプル」を参照してください。

この例は、Extensible Application Markup Language (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);
    }
}

サンプル全体については、「マージンの設定のサンプル」を参照してください。