Alignment、Margin 和 Padding 概述

FrameworkElement 类公开一些用于精确定位子元素的属性。 本主题论述其中四个最重要的属性:HorizontalAlignmentMarginPaddingVerticalAlignment。 务必要了解这些属性的作用,因为这些属性是控制元素在 Windows Presentation Foundation (WPF) 应用程序中的位置的基础。

本主题包括下列各节。

  • 元素定位简介
  • 了解 Alignment 属性
  • 了解 Margin 属性
  • 了解 Padding 属性
  • 在应用程序中使用 Alignment、Margins 和 Padding
  • 接下来的内容
  • 相关主题

元素定位简介

可以使用 WPF 通过多种方式来定位元素。 但是,获得理想的布局不仅要选择正确的 Panel 元素。 需要了解 HorizontalAlignmentMarginPaddingVerticalAlignment 属性才能实现精确的定位控制。

下图显示了一个采用若干定位属性的布局方案。

WPF 定位属性示例

乍看上去,此图中的 Button 元素可能似乎是随意放置的。 但是,它们的位置实际上是通过使用边距、对齐和空白的组合加以精确控制的。

下面的示例描述如何创建上图中的布局。 Border 元素封装了父 StackPanelPadding 值为 15 个与设备无关的像素。 这就是子 StackPanel 周围会有 LightBlue 窄带的原因。 StackPanel 的子元素用于举例说明本主题中详细论述的各个不同的定位属性。 将使用三个 Button 元素来演示 MarginHorizontalAlignment 属性。

WindowTitle = "Margins, Padding and Alignment Sample"

'Add a Border.
Dim myBorder As New Border()
myBorder.Background = Brushes.LightBlue
myBorder.BorderBrush = Brushes.Black
myBorder.Padding = New Thickness(15)
myBorder.BorderThickness = New Thickness(2)

Dim myStackPanel As New StackPanel()
myStackPanel.Background = Brushes.White
myStackPanel.HorizontalAlignment = Windows.HorizontalAlignment.Center
myStackPanel.VerticalAlignment = Windows.VerticalAlignment.Top

Dim myTextBlock As New TextBlock()
myTextBlock.Margin = New Thickness(5, 0, 5, 0)
myTextBlock.FontSize = 18
myTextBlock.HorizontalAlignment = Windows.HorizontalAlignment.Center
myTextBlock.Text = "Alignment, Margin and Padding Sample"
Dim myButton1 As New Button()
myButton1.HorizontalAlignment = Windows.HorizontalAlignment.Left
myButton1.Margin = New Thickness(20)
myButton1.Content = "Button 1"
Dim myButton2 As New Button()
myButton2.HorizontalAlignment = Windows.HorizontalAlignment.Right
myButton2.Margin = New Thickness(10)
myButton2.Content = "Button 2"
Dim myButton3 As New Button()
myButton3.HorizontalAlignment = Windows.HorizontalAlignment.Stretch
myButton3.Margin = New Thickness(0)
myButton3.Content = "Button 3"

'Add child elements to the parent StackPanel.
myStackPanel.Children.Add(myTextBlock)
myStackPanel.Children.Add(myButton1)
myStackPanel.Children.Add(myButton2)
myStackPanel.Children.Add(myButton3)

'Add the StackPanel as the lone Child of the Border.
myBorder.Child = myStackPanel

' Add the Canvas as the lone Child of the Border
myBorder.Child = myStackPanel
Me.Content = myBorder

            // Create the application's main Window.
            mainWindow = new Window ();
            mainWindow.Title = "Margins, Padding and Alignment Sample";

            // Add a Border
            myBorder = new Border();
            myBorder.Background = Brushes.LightBlue;
            myBorder.BorderBrush = Brushes.Black;
            myBorder.Padding = new Thickness(15);
            myBorder.BorderThickness = new Thickness(2);

            myStackPanel = new StackPanel();
            myStackPanel.Background = Brushes.White;
            myStackPanel.HorizontalAlignment = HorizontalAlignment.Center;
            myStackPanel.VerticalAlignment = VerticalAlignment.Top;

            TextBlock myTextBlock = new TextBlock();
            myTextBlock.Margin = new Thickness(5, 0, 5, 0);
            myTextBlock.FontSize = 18;
            myTextBlock.HorizontalAlignment = HorizontalAlignment.Center;
            myTextBlock.Text = "Alignment, Margin and Padding Sample";
            Button myButton1 = new Button();
            myButton1.HorizontalAlignment = HorizontalAlignment.Left;
            myButton1.Margin = new Thickness(20);
            myButton1.Content = "Button 1";
            Button myButton2 = new Button();
            myButton2.HorizontalAlignment = HorizontalAlignment.Right;
            myButton2.Margin = new Thickness(10);
            myButton2.Content = "Button 2";
            Button myButton3 = new Button();
            myButton3.HorizontalAlignment = HorizontalAlignment.Stretch;
            myButton3.Margin = new Thickness(0);
            myButton3.Content = "Button 3";

            // Add child elements to the parent StackPanel.
            myStackPanel.Children.Add(myTextBlock);
            myStackPanel.Children.Add(myButton1);
            myStackPanel.Children.Add(myButton2);
            myStackPanel.Children.Add(myButton3);

            // Add the StackPanel as the lone Child of the Border.
            myBorder.Child = myStackPanel;

            // Add the Border as the Content of the Parent Window Object.
            mainWindow.Content = myBorder;
            mainWindow.Show ();

<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" WindowTitle="Margins, Padding and Alignment Sample">
  <Border Background="LightBlue" BorderBrush="Black" BorderThickness="2" Padding="15">
    <StackPanel Background="White" HorizontalAlignment="Center" VerticalAlignment="Top">
        <TextBlock Margin="5,0,5,0" FontSize="18" HorizontalAlignment="Center">Alignment, Margin and Padding Sample</TextBlock>
        <Button HorizontalAlignment="Left" Margin="20">Button 1</Button>
        <Button HorizontalAlignment="Right" Margin="10">Button 2</Button>
        <Button HorizontalAlignment="Stretch" Margin="0">Button 3</Button>
    </StackPanel> 
  </Border>    
</Page>

通过下图可以仔细查看前面的示例中使用的各个定位属性。 本主题中后面的各节将更详细地介绍如何使用各个定位属性。

具有屏幕标注的定位属性

了解 Alignment 属性

HorizontalAlignmentVerticalAlignment 属性描述应如何在父元素的已分配布局空间中定位子元素。 通过将这些属性结合使用,您可以精确地定位子元素。 例如,DockPanel 的子元素可以指定四种不同的水平对齐方式:LeftRightCenter,或者 Stretch 以填充可用空间。 类似的值可用于垂直定位。

注意注意

在一个元素上显式设置的 HeightWidth 属性优先于 Stretch 属性值。如果尝试设置 Height, Width 以及 Stretch 的 HorizontalAlignment 值,将会导致 Stretch 请求被忽略。

HorizontalAlignment 属性

HorizontalAlignment 属性声明要应用于子元素的水平对齐特性。 下表列出了 HorizontalAlignment 属性的每个可能的值。

成员

说明

Left

子元素与父元素的已分配布局空间的左端对齐。

Center

子元素与父元素的已分配布局空间的中心对齐。

Right

子元素与父元素的已分配布局空间的右端对齐。

Stretch(默认值)

拉伸子元素以填充父元素的已分配布局空间。 显式的 WidthHeight 值优先。

下面的示例演示如何将 HorizontalAlignment 属性应用于 Button 元素。 将显示每个特性值,以便更好地阐释各种呈现行为。

Dim myButton1 As New Button()
myButton1.HorizontalAlignment = Windows.HorizontalAlignment.Left
myButton1.Margin = New Thickness(20)
myButton1.Content = "Button 1"
Dim myButton2 As New Button()
myButton2.HorizontalAlignment = Windows.HorizontalAlignment.Right
myButton2.Margin = New Thickness(10)
myButton2.Content = "Button 2"
Dim myButton3 As New Button()
myButton3.HorizontalAlignment = Windows.HorizontalAlignment.Center
myButton3.Margin = New Thickness(0)
myButton3.Content = "Button 3"
Dim myButton4 As New Button()
myButton4.HorizontalAlignment = Windows.HorizontalAlignment.Stretch
myButton4.Content = "Button 4 (Stretch)"
Button myButton1 = new Button();
myButton1.HorizontalAlignment = HorizontalAlignment.Left;
myButton1.Content = "Button 1 (Left)";
Button myButton2 = new Button();
myButton2.HorizontalAlignment = HorizontalAlignment.Right;
myButton2.Content = "Button 2 (Right)";
Button myButton3 = new Button();
myButton3.HorizontalAlignment = HorizontalAlignment.Center;
myButton3.Content = "Button 3 (Center)";
Button myButton4 = new Button();
myButton4.HorizontalAlignment = HorizontalAlignment.Stretch;
myButton4.Content = "Button 4 (Stretch)";
<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" WindowTitle="HorizontalAlignment Sample">
  <Border Background="LightBlue" BorderBrush="Black" BorderThickness="2" Padding="15">
    <StackPanel Background="White" HorizontalAlignment="Center" VerticalAlignment="Top">
        <TextBlock Margin="5,0,5,0" FontSize="18" HorizontalAlignment="Center">HorizontalAlignment Sample</TextBlock>
    <Button HorizontalAlignment="Left">Button 1 (Left)</Button>
    <Button HorizontalAlignment="Right">Button 2 (Right)</Button>
    <Button HorizontalAlignment="Center">Button 3 (Center)</Button>
    <Button HorizontalAlignment="Stretch">Button 4 (Stretch)</Button>

    </StackPanel> 
  </Border>    
</Page>

以上代码将产生与下图类似的布局。 在图中可以看到每个 HorizontalAlignment 值的定位效果。

HorizontalAlignment 示例

VerticalAlignment 属性

VerticalAlignment 属性描述要应用于子元素的垂直对齐特性。 下表列出了 VerticalAlignment 属性的每个可能的值。

成员

说明

Top

子元素与父元素的已分配布局空间的顶端对齐。

Center

子元素与父元素的已分配布局空间的中心对齐。

Bottom

子元素与父元素的已分配布局空间的底端对齐。

Stretch(默认值)

拉伸子元素以填充父元素的已分配布局空间。 显式的 WidthHeight 值优先。

下面的示例演示如何将 VerticalAlignment 属性应用于 Button 元素。 将显示每个特性值,以便更好地阐释各种呈现行为。 在此示例中,将使用带有可见网格线的 Grid 元素作为父项,以便更好地阐释各个属性值的布局行为。

Dim myTextBlock As New TextBlock()
myTextBlock.FontSize = 18
myTextBlock.HorizontalAlignment = Windows.HorizontalAlignment.Center
myTextBlock.Text = "VerticalAlignment Sample"
Grid.SetRow(myTextBlock, 0)
Dim myButton1 As New Button()
myButton1.VerticalAlignment = Windows.VerticalAlignment.Top
myButton1.Content = "Button 1 (Top)"
Grid.SetRow(myButton1, 1)
Dim myButton2 As New Button()
myButton2.VerticalAlignment = Windows.VerticalAlignment.Bottom
myButton2.Content = "Button 2 (Bottom)"
Grid.SetRow(myButton2, 2)
Dim myButton3 As New Button()
myButton3.VerticalAlignment = Windows.VerticalAlignment.Center
myButton3.Content = "Button 3 (Center)"
Grid.SetRow(myButton3, 3)
Dim myButton4 As New Button()
myButton4.VerticalAlignment = Windows.VerticalAlignment.Stretch
myButton4.Content = "Button 4 (Stretch)"
Grid.SetRow(myButton4, 4)
TextBlock myTextBlock = new TextBlock();
myTextBlock.FontSize = 18;
myTextBlock.HorizontalAlignment = HorizontalAlignment.Center;
myTextBlock.Text = "VerticalAlignment Sample";
Grid.SetRow(myTextBlock, 0);
Button myButton1 = new Button();
myButton1.VerticalAlignment = VerticalAlignment.Top;
myButton1.Content = "Button 1 (Top)";
Grid.SetRow(myButton1, 1);
Button myButton2 = new Button();
myButton2.VerticalAlignment = VerticalAlignment.Bottom;
myButton2.Content = "Button 2 (Bottom)";
Grid.SetRow(myButton2, 2);
Button myButton3 = new Button();
myButton3.VerticalAlignment = VerticalAlignment.Center;
myButton3.Content = "Button 3 (Center)";
Grid.SetRow(myButton3, 3);
Button myButton4 = new Button();
myButton4.VerticalAlignment = VerticalAlignment.Stretch;
myButton4.Content = "Button 4 (Stretch)";
Grid.SetRow(myButton4, 4);
<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
      WindowTitle="VerticalAlignment Sample">
  <Border Background="LightBlue" BorderBrush="Black" BorderThickness="2" Padding="15">
    <Grid Background="White" ShowGridLines="True">
      <Grid.RowDefinitions>
        <RowDefinition Height="25"/>
        <RowDefinition Height="50"/>
        <RowDefinition Height="50"/>
        <RowDefinition Height="50"/>
        <RowDefinition Height="50"/>
      </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" Grid.Column="0" FontSize="18" HorizontalAlignment="Center">VerticalAlignment Sample</TextBlock>
            <Button Grid.Row="1" Grid.Column="0" VerticalAlignment="Top">Button 1 (Top)</Button>
            <Button Grid.Row="2" Grid.Column="0" VerticalAlignment="Bottom">Button 2 (Bottom)</Button>    
            <Button Grid.Row="3" Grid.Column="0" VerticalAlignment="Center">Button 3 (Center)</Button>
            <Button Grid.Row="4" Grid.Column="0" VerticalAlignment="Stretch">Button 4 (Stretch)</Button>          
    </Grid>
  </Border>    
</Page>

以上代码将产生与下图类似的布局。 在图中可以看到每个 VerticalAlignment 值的定位效果。

VerticalAlignment 属性示例

了解 Margin 属性

Margin 属性描述元素与其子级或同级之间的距离。 可通过使用 Margin="20" 这样的语法使 Margin 值统一。 利用此语法,将向元素应用 20 个与设备无关的像素的统一 MarginMargin 值也可以采用四个不同值的形式,每个值描述应用于左端、顶端、右端和底端(按该顺序应用)的不同边距,比如 Margin="0,10,5,25"。 通过恰当地使用 Margin 属性,将可以非常精确地控制元素的呈现位置,以及元素的邻近元素和子项的呈现位置。

注意注意

非零的边距会在元素的 ActualWidthActualHeight 外部产生空白。

下面的示例演示如何在一组 Button 元素的周围应用统一的边距。 Button 元素各个方向的间距都相同,并带有 10 像素的边距缓冲区。

Dim myButton7 As New Button
myButton7.Margin = New Thickness(10)
myButton7.Content = "Button 7"
Dim myButton8 As New Button
myButton8.Margin = New Thickness(10)
myButton8.Content = "Button 8"
Dim myButton9 As New Button
myButton9.Margin = New Thickness(10)
myButton9.Content = "Button 9"
Button myButton7 = new Button();
myButton7.Margin = new Thickness(10);
myButton7.Content = "Button 7";
Button myButton8 = new Button();
myButton8.Margin = new Thickness(10);
myButton8.Content = "Button 8";
Button myButton9 = new Button();
myButton9.Margin = new Thickness(10);
myButton9.Content = "Button 9";
Button^ myButton7 = gcnew Button();
myButton7->Margin = Thickness(10);
myButton7->Content = "Button 7";
Button^ myButton8 = gcnew Button();
myButton8->Margin = Thickness(10);
myButton8->Content = "Button 8";
Button^ myButton9 = gcnew Button();
myButton9->Margin = Thickness(10);
myButton9->Content = "Button 9";
<Button Margin="10">Button 7</Button>
<Button Margin="10">Button 8</Button>
<Button Margin="10">Button 9</Button>

在许多情况下,统一的边距并不适宜。 对于这些情况,可以应用非统一间距。 下面的示例演示如何将非统一边距间距应用于子元素。 按此顺序描述边距:左端、顶端、右端和底端。

Dim myButton1 As New Button
myButton1.Margin = New Thickness(0, 10, 0, 10)
myButton1.Content = "Button 1"
Dim myButton2 As New Button
myButton2.Margin = New Thickness(0, 10, 0, 10)
myButton2.Content = "Button 2"
Dim myButton3 As New Button
myButton3.Margin = New Thickness(0, 10, 0, 10)
Button myButton1 = new Button();
myButton1.Margin = new Thickness(0, 10, 0, 10);
myButton1.Content = "Button 1";
Button myButton2 = new Button();
myButton2.Margin = new Thickness(0, 10, 0, 10);
myButton2.Content = "Button 2";
Button myButton3 = new Button();
myButton3.Margin = new Thickness(0, 10, 0, 10);
Button^ myButton1 = gcnew Button();
myButton1->Margin = Thickness(0, 10, 0, 10);
myButton1->Content = "Button 1";
Button^ myButton2 = gcnew Button();
myButton2->Margin = Thickness(0, 10, 0, 10);
myButton2->Content = "Button 2";
Button^ myButton3 = gcnew Button();
myButton3->Margin = Thickness(0, 10, 0, 10);
<Button Margin="0,10,0,10">Button 1</Button>
<Button Margin="0,10,0,10">Button 2</Button>
<Button Margin="0,10,0,10">Button 3</Button>

了解 Padding 属性

Padding 在大多数方面类似于 Margin。 Padding 属性只会在少数类上公开,主要是为了方便起见而公开:BlockBorderControlTextBlock 是公开 Padding 属性的类的示例。 Padding 属性可将子元素的有效大小增大指定的 Thickness 值。

下面的示例演示如何将 Padding 应用于父 Border 元素。

Dim myBorder As New Border
myBorder.Background = Brushes.LightBlue
myBorder.BorderBrush = Brushes.Black
myBorder.BorderThickness = New Thickness(2)
myBorder.CornerRadius = New CornerRadius(45)
myBorder.Padding = New Thickness(25)
myBorder = new Border();
myBorder.Background = Brushes.LightBlue;
myBorder.BorderBrush = Brushes.Black;
myBorder.BorderThickness = new Thickness(2);
myBorder.CornerRadius = new CornerRadius(45);
myBorder.Padding = new Thickness(25);
myBorder = gcnew Border();
myBorder->Background = Brushes::LightBlue;
myBorder->BorderBrush = Brushes::Black;
myBorder->BorderThickness = Thickness(2);
myBorder->CornerRadius = CornerRadius(45);
myBorder->Padding = Thickness(25);
<Border Background="LightBlue" 
        BorderBrush="Black" 
        BorderThickness="2" 
        CornerRadius="45" 
        Padding="25">

在应用程序中使用 Alignment、Margins 和 Padding

HorizontalAlignmentMarginPaddingVerticalAlignment 提供了创建复杂的 user interface (UI) 必不可少的定位控制。 您可以利用每个属性的作用来更改子元素定位,从而能够灵活地创建动态的应用程序和用户体验。

下面的示例演示本主题中详细论述的各个概念。 此示例以本主题第一个示例中的基础结构为基础,添加了一个 Grid 元素作为第一个示例中 Border 的子项。 Padding 应用于父 Border 元素。 Grid 用于划分三个子 StackPanel 元素之间的空间。 将再次使用 Button 元素显示 MarginHorizontalAlignment 的不同效果。 将向每个 ColumnDefinition 中添加 TextBlock 元素,以便更好地定义应用于各列中 Button 元素的不同属性。


            Dim myBorder As New Border
            myBorder.Background = Brushes.LightBlue
            myBorder.BorderBrush = Brushes.Black
            myBorder.BorderThickness = New Thickness(2)
            myBorder.CornerRadius = New CornerRadius(45)
            myBorder.Padding = New Thickness(25)

            'Define the Grid.
            Dim myGrid As New Grid
            myGrid.Background = Brushes.White
            myGrid.ShowGridLines = True

            'Define the Columns.
            Dim myColDef1 As New ColumnDefinition
            myColDef1.Width = New GridLength(1, GridUnitType.Auto)
            Dim myColDef2 As New ColumnDefinition
            myColDef2.Width = New GridLength(1, GridUnitType.Star)
            Dim myColDef3 As New ColumnDefinition
            myColDef3.Width = New GridLength(1, GridUnitType.Auto)

            'Add the ColumnDefinitions to the Grid
            myGrid.ColumnDefinitions.Add(myColDef1)
            myGrid.ColumnDefinitions.Add(myColDef2)
            myGrid.ColumnDefinitions.Add(myColDef3)

            'Add the first child StackPanel.
            Dim myStackPanel As New StackPanel
            myStackPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Left
            myStackPanel.VerticalAlignment = System.Windows.VerticalAlignment.Top
            Grid.SetColumn(myStackPanel, 0)
            Grid.SetRow(myStackPanel, 0)
            Dim myTextBlock1 As New TextBlock
            myTextBlock1.FontSize = 18
            myTextBlock1.HorizontalAlignment = System.Windows.HorizontalAlignment.Center
            myTextBlock1.Margin = New Thickness(0, 0, 0, 15)
            myTextBlock1.Text = "StackPanel 1"

            Dim myButton1 As New Button
            myButton1.Margin = New Thickness(0, 10, 0, 10)
            myButton1.Content = "Button 1"
            Dim myButton2 As New Button
            myButton2.Margin = New Thickness(0, 10, 0, 10)
            myButton2.Content = "Button 2"
            Dim myButton3 As New Button
            myButton3.Margin = New Thickness(0, 10, 0, 10)

            Dim myTextBlock2 As New TextBlock
            myTextBlock2.Text = "ColumnDefinition.Width = ""Auto"""
            Dim myTextBlock3 As New TextBlock
            myTextBlock3.Text = "StackPanel.HorizontalAlignment = ""Left"""
            Dim myTextBlock4 As New TextBlock
            myTextBlock4.Text = "StackPanel.VerticalAlignment = ""Top"""
            Dim myTextBlock5 As New TextBlock
            myTextBlock5.Text = "StackPanel.Orientation = ""Vertical"""
            Dim myTextBlock6 As New TextBlock
            myTextBlock6.Text = "Button.Margin = ""1,10,0,10"""
            myStackPanel.Children.Add(myTextBlock1)
            myStackPanel.Children.Add(myButton1)
            myStackPanel.Children.Add(myButton2)
            myStackPanel.Children.Add(myButton3)
            myStackPanel.Children.Add(myTextBlock2)
            myStackPanel.Children.Add(myTextBlock3)
            myStackPanel.Children.Add(myTextBlock4)
            myStackPanel.Children.Add(myTextBlock5)
            myStackPanel.Children.Add(myTextBlock6)

            'Add the second child StackPanel.
            Dim myStackPanel2 As New StackPanel
            myStackPanel2.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch
            myStackPanel2.VerticalAlignment = System.Windows.VerticalAlignment.Top
            myStackPanel2.Orientation = Orientation.Vertical
            Grid.SetColumn(myStackPanel2, 1)
            Grid.SetRow(myStackPanel2, 0)
            Dim myTextBlock7 As New TextBlock
            myTextBlock7.FontSize = 18
            myTextBlock7.HorizontalAlignment = System.Windows.HorizontalAlignment.Center
            myTextBlock7.Margin = New Thickness(0, 0, 0, 15)
            myTextBlock7.Text = "StackPanel 2"
            Dim myButton4 As New Button
            myButton4.Margin = New Thickness(10, 0, 10, 0)
            myButton4.Content = "Button 4"
            Dim myButton5 As New Button
            myButton5.Margin = New Thickness(10, 0, 10, 0)
            myButton5.Content = "Button 5"
            Dim myButton6 As New Button
            myButton6.Margin = New Thickness(10, 0, 10, 0)
            myButton6.Content = "Button 6"
            Dim myTextBlock8 As New TextBlock
            myTextBlock8.HorizontalAlignment = System.Windows.HorizontalAlignment.Center
            myTextBlock8.Text = "ColumnDefinition.Width = ""*"""
            Dim myTextBlock9 As New TextBlock
            myTextBlock9.HorizontalAlignment = System.Windows.HorizontalAlignment.Center
            myTextBlock9.Text = "StackPanel.HorizontalAlignment = ""Stretch"""
            Dim myTextBlock10 As New TextBlock
            myTextBlock10.HorizontalAlignment = System.Windows.HorizontalAlignment.Center
            myTextBlock10.Text = "StackPanel.VerticalAlignment = ""Top"""
            Dim myTextBlock11 As New TextBlock
            myTextBlock11.HorizontalAlignment = System.Windows.HorizontalAlignment.Center
            myTextBlock11.Text = "StackPanel.Orientation = ""Horizontal"""
            Dim myTextBlock12 As New TextBlock
            myTextBlock12.HorizontalAlignment = System.Windows.HorizontalAlignment.Center
            myTextBlock12.Text = "Button.Margin = ""10,0,10,0"""
            myStackPanel2.Children.Add(myTextBlock7)
            myStackPanel2.Children.Add(myButton4)
            myStackPanel2.Children.Add(myButton5)
            myStackPanel2.Children.Add(myButton6)
            myStackPanel2.Children.Add(myTextBlock8)
            myStackPanel2.Children.Add(myTextBlock9)
            myStackPanel2.Children.Add(myTextBlock10)
            myStackPanel2.Children.Add(myTextBlock11)
            myStackPanel2.Children.Add(myTextBlock12)

            'Add the final child StackPanel.
            Dim myStackPanel3 As New StackPanel
            myStackPanel3.HorizontalAlignment = System.Windows.HorizontalAlignment.Left
            myStackPanel3.VerticalAlignment = System.Windows.VerticalAlignment.Top
            Grid.SetColumn(myStackPanel3, 2)
            Grid.SetRow(myStackPanel3, 0)
            Dim myTextBlock13 As New TextBlock
            myTextBlock13.FontSize = 18
            myTextBlock13.HorizontalAlignment = System.Windows.HorizontalAlignment.Center
            myTextBlock13.Margin = New Thickness(0, 0, 0, 15)
            myTextBlock13.Text = "StackPanel 3"

            Dim myButton7 As New Button
            myButton7.Margin = New Thickness(10)
            myButton7.Content = "Button 7"
            Dim myButton8 As New Button
            myButton8.Margin = New Thickness(10)
            myButton8.Content = "Button 8"
            Dim myButton9 As New Button
            myButton9.Margin = New Thickness(10)
            myButton9.Content = "Button 9"
            Dim myTextBlock14 As New TextBlock
            myTextBlock14.Text = "ColumnDefinition.Width = ""Auto"""
            Dim myTextBlock15 As New TextBlock
            myTextBlock15.Text = "StackPanel.HorizontalAlignment = ""Left"""
            Dim myTextBlock16 As New TextBlock
            myTextBlock16.Text = "StackPanel.VerticalAlignment = ""Top"""
            Dim myTextBlock17 As New TextBlock
            myTextBlock17.Text = "StackPanel.Orientation = ""Vertical"""
            Dim myTextBlock18 As New TextBlock
            myTextBlock18.Text = "Button.Margin = ""10"""
            myStackPanel3.Children.Add(myTextBlock13)
            myStackPanel3.Children.Add(myButton7)
            myStackPanel3.Children.Add(myButton8)
            myStackPanel3.Children.Add(myButton9)
            myStackPanel3.Children.Add(myTextBlock14)
            myStackPanel3.Children.Add(myTextBlock15)
            myStackPanel3.Children.Add(myTextBlock16)
            myStackPanel3.Children.Add(myTextBlock17)
            myStackPanel3.Children.Add(myTextBlock18)

            'Add child content to the parent Grid.
            myGrid.Children.Add(myStackPanel)
            myGrid.Children.Add(myStackPanel2)
            myGrid.Children.Add(myStackPanel3)

            'Add the Grid as the lone child of the Border.
            myBorder.Child = myGrid

mainWindow = new Window();

myBorder = new Border();
myBorder.Background = Brushes.LightBlue;
myBorder.BorderBrush = Brushes.Black;
myBorder.BorderThickness = new Thickness(2);
myBorder.CornerRadius = new CornerRadius(45);
myBorder.Padding = new Thickness(25);

// Define the Grid.
myGrid = new Grid();
myGrid.Background = Brushes.White;
myGrid.ShowGridLines = true;

// Define the Columns.
ColumnDefinition myColDef1 = new ColumnDefinition();
myColDef1.Width = new GridLength(1, GridUnitType.Auto);
ColumnDefinition myColDef2 = new ColumnDefinition();
myColDef2.Width = new GridLength(1, GridUnitType.Star);
ColumnDefinition myColDef3 = new ColumnDefinition();
myColDef3.Width = new GridLength(1, GridUnitType.Auto);

// Add the ColumnDefinitions to the Grid.
myGrid.ColumnDefinitions.Add(myColDef1);
myGrid.ColumnDefinitions.Add(myColDef2);
myGrid.ColumnDefinitions.Add(myColDef3);

// Add the first child StackPanel.
StackPanel myStackPanel = new StackPanel();
myStackPanel.HorizontalAlignment = HorizontalAlignment.Left;
myStackPanel.VerticalAlignment = VerticalAlignment.Top;
Grid.SetColumn(myStackPanel, 0);
Grid.SetRow(myStackPanel, 0);
TextBlock myTextBlock1 = new TextBlock();
myTextBlock1.FontSize = 18;
myTextBlock1.HorizontalAlignment = HorizontalAlignment.Center;
myTextBlock1.Margin = new Thickness(0, 0, 0, 15);
myTextBlock1.Text = "StackPanel 1";
Button myButton1 = new Button();
myButton1.Margin = new Thickness(0, 10, 0, 10);
myButton1.Content = "Button 1";
Button myButton2 = new Button();
myButton2.Margin = new Thickness(0, 10, 0, 10);
myButton2.Content = "Button 2";
Button myButton3 = new Button();
myButton3.Margin = new Thickness(0, 10, 0, 10);
TextBlock myTextBlock2 = new TextBlock();
myTextBlock2.Text = @"ColumnDefinition.Width = ""Auto""";
TextBlock myTextBlock3 = new TextBlock();
myTextBlock3.Text = @"StackPanel.HorizontalAlignment = ""Left""";
TextBlock myTextBlock4 = new TextBlock();
myTextBlock4.Text = @"StackPanel.VerticalAlignment = ""Top""";
TextBlock myTextBlock5 = new TextBlock();
myTextBlock5.Text = @"StackPanel.Orientation = ""Vertical""";
TextBlock myTextBlock6 = new TextBlock();
myTextBlock6.Text = @"Button.Margin = ""1,10,0,10""";
myStackPanel.Children.Add(myTextBlock1);
myStackPanel.Children.Add(myButton1);
myStackPanel.Children.Add(myButton2);
myStackPanel.Children.Add(myButton3);
myStackPanel.Children.Add(myTextBlock2);
myStackPanel.Children.Add(myTextBlock3);
myStackPanel.Children.Add(myTextBlock4);
myStackPanel.Children.Add(myTextBlock5);
myStackPanel.Children.Add(myTextBlock6);

// Add the second child StackPanel.
StackPanel myStackPanel2 = new StackPanel();
myStackPanel2.HorizontalAlignment = HorizontalAlignment.Stretch;
myStackPanel2.VerticalAlignment = VerticalAlignment.Top;
myStackPanel2.Orientation = Orientation.Vertical;
Grid.SetColumn(myStackPanel2, 1);
Grid.SetRow(myStackPanel2, 0);
TextBlock myTextBlock7 = new TextBlock();
myTextBlock7.FontSize = 18;
myTextBlock7.HorizontalAlignment = HorizontalAlignment.Center;
myTextBlock7.Margin = new Thickness(0, 0, 0, 15);
myTextBlock7.Text = "StackPanel 2";
Button myButton4 = new Button();
myButton4.Margin = new Thickness(10, 0, 10, 0);
myButton4.Content = "Button 4";
Button myButton5 = new Button();
myButton5.Margin = new Thickness(10, 0, 10, 0);
myButton5.Content = "Button 5";
Button myButton6 = new Button();
myButton6.Margin = new Thickness(10, 0, 10, 0);
myButton6.Content = "Button 6";
TextBlock myTextBlock8 = new TextBlock();
myTextBlock8.HorizontalAlignment = HorizontalAlignment.Center;
myTextBlock8.Text = @"ColumnDefinition.Width = ""*""";
TextBlock myTextBlock9 = new TextBlock();
myTextBlock9.HorizontalAlignment = HorizontalAlignment.Center;
myTextBlock9.Text = @"StackPanel.HorizontalAlignment = ""Stretch""";
TextBlock myTextBlock10 = new TextBlock();
myTextBlock10.HorizontalAlignment = HorizontalAlignment.Center;
myTextBlock10.Text = @"StackPanel.VerticalAlignment = ""Top""";
TextBlock myTextBlock11 = new TextBlock();
myTextBlock11.HorizontalAlignment = HorizontalAlignment.Center;
myTextBlock11.Text = @"StackPanel.Orientation = ""Horizontal""";
TextBlock myTextBlock12 = new TextBlock();
myTextBlock12.HorizontalAlignment = HorizontalAlignment.Center;
myTextBlock12.Text = @"Button.Margin = ""10,0,10,0""";
myStackPanel2.Children.Add(myTextBlock7);
myStackPanel2.Children.Add(myButton4);
myStackPanel2.Children.Add(myButton5);
myStackPanel2.Children.Add(myButton6);
myStackPanel2.Children.Add(myTextBlock8);
myStackPanel2.Children.Add(myTextBlock9);
myStackPanel2.Children.Add(myTextBlock10);
myStackPanel2.Children.Add(myTextBlock11);
myStackPanel2.Children.Add(myTextBlock12);

// Add the final child StackPanel.
StackPanel myStackPanel3 = new StackPanel();
myStackPanel3.HorizontalAlignment = HorizontalAlignment.Left;
myStackPanel3.VerticalAlignment = VerticalAlignment.Top;
Grid.SetColumn(myStackPanel3, 2);
Grid.SetRow(myStackPanel3, 0);
TextBlock myTextBlock13 = new TextBlock();
myTextBlock13.FontSize = 18;
myTextBlock13.HorizontalAlignment = HorizontalAlignment.Center;
myTextBlock13.Margin = new Thickness(0, 0, 0, 15);
myTextBlock13.Text = "StackPanel 3";
Button myButton7 = new Button();
myButton7.Margin = new Thickness(10);
myButton7.Content = "Button 7";
Button myButton8 = new Button();
myButton8.Margin = new Thickness(10);
myButton8.Content = "Button 8";
Button myButton9 = new Button();
myButton9.Margin = new Thickness(10);
myButton9.Content = "Button 9";
TextBlock myTextBlock14 = new TextBlock();
myTextBlock14.Text = @"ColumnDefinition.Width = ""Auto""";
TextBlock myTextBlock15 = new TextBlock();
myTextBlock15.Text = @"StackPanel.HorizontalAlignment = ""Left""";
TextBlock myTextBlock16 = new TextBlock();
myTextBlock16.Text = @"StackPanel.VerticalAlignment = ""Top""";
TextBlock myTextBlock17 = new TextBlock();
myTextBlock17.Text = @"StackPanel.Orientation = ""Vertical""";
TextBlock myTextBlock18 = new TextBlock();
myTextBlock18.Text = @"Button.Margin = ""10""";
myStackPanel3.Children.Add(myTextBlock13);
myStackPanel3.Children.Add(myButton7);
myStackPanel3.Children.Add(myButton8);
myStackPanel3.Children.Add(myButton9);
myStackPanel3.Children.Add(myTextBlock14);
myStackPanel3.Children.Add(myTextBlock15);
myStackPanel3.Children.Add(myTextBlock16);
myStackPanel3.Children.Add(myTextBlock17);
myStackPanel3.Children.Add(myTextBlock18);

// Add child content to the parent Grid.
myGrid.Children.Add(myStackPanel);
myGrid.Children.Add(myStackPanel2);
myGrid.Children.Add(myStackPanel3);

// Add the Grid as the lone child of the Border.
myBorder.Child = myGrid;

// Add the Border to the Window as Content and show the Window.
mainWindow.Content = myBorder;
mainWindow.Title = "Margin, Padding, and Alignment Sample";
mainWindow.Show();
mainWindow = gcnew Window();

myBorder = gcnew Border();
myBorder->Background = Brushes::LightBlue;
myBorder->BorderBrush = Brushes::Black;
myBorder->BorderThickness = Thickness(2);
myBorder->CornerRadius = CornerRadius(45);
myBorder->Padding = Thickness(25);

// Define the Grid.
myGrid = gcnew Grid();
myGrid->Background = Brushes::White;
myGrid->ShowGridLines = true;

// Define the Columns.
ColumnDefinition^ myColDef1 = gcnew ColumnDefinition();
myColDef1->Width = GridLength(1, GridUnitType::Auto);
ColumnDefinition^ myColDef2 = gcnew ColumnDefinition();
myColDef2->Width = GridLength(1, GridUnitType::Star);
ColumnDefinition^ myColDef3 = gcnew ColumnDefinition();
myColDef3->Width = GridLength(1, GridUnitType::Auto);

// Add the ColumnDefinitions to the Grid.
myGrid->ColumnDefinitions->Add(myColDef1);
myGrid->ColumnDefinitions->Add(myColDef2);
myGrid->ColumnDefinitions->Add(myColDef3);

// Add the first child StackPanel.
StackPanel^ myStackPanel = gcnew StackPanel();
myStackPanel->HorizontalAlignment = HorizontalAlignment::Left;
myStackPanel->VerticalAlignment = VerticalAlignment::Top;
Grid::SetColumn(myStackPanel, 0);
Grid::SetRow(myStackPanel, 0);
TextBlock^ myTextBlock1 = gcnew TextBlock();
myTextBlock1->FontSize = 18;
myTextBlock1->HorizontalAlignment = HorizontalAlignment::Center;
myTextBlock1->Margin = Thickness(0, 0, 0, 15);
myTextBlock1->Text = "StackPanel 1";
Button^ myButton1 = gcnew Button();
myButton1->Margin = Thickness(0, 10, 0, 10);
myButton1->Content = "Button 1";
Button^ myButton2 = gcnew Button();
myButton2->Margin = Thickness(0, 10, 0, 10);
myButton2->Content = "Button 2";
Button^ myButton3 = gcnew Button();
myButton3->Margin = Thickness(0, 10, 0, 10);
TextBlock^ myTextBlock2 = gcnew TextBlock();
myTextBlock2->Text = "ColumnDefinition.Width = \"Auto\"";
TextBlock^ myTextBlock3 = gcnew TextBlock();
myTextBlock3->Text = "StackPanel.HorizontalAlignment = \"Left\"";
TextBlock^ myTextBlock4 = gcnew TextBlock();
myTextBlock4->Text = "StackPanel.VerticalAlignment = \"Top\"";
TextBlock^ myTextBlock5 = gcnew TextBlock();
myTextBlock5->Text = "StackPanel.Orientation = \"Vertical\"";
TextBlock^ myTextBlock6 = gcnew TextBlock();
myTextBlock6->Text = "Button.Margin = \"1,10,0,10\"";
myStackPanel->Children->Add(myTextBlock1);
myStackPanel->Children->Add(myButton1);
myStackPanel->Children->Add(myButton2);
myStackPanel->Children->Add(myButton3);
myStackPanel->Children->Add(myTextBlock2);
myStackPanel->Children->Add(myTextBlock3);
myStackPanel->Children->Add(myTextBlock4);
myStackPanel->Children->Add(myTextBlock5);
myStackPanel->Children->Add(myTextBlock6);

// Add the second child StackPanel.
StackPanel^ myStackPanel2 = gcnew StackPanel();
myStackPanel2->HorizontalAlignment = HorizontalAlignment::Stretch;
myStackPanel2->VerticalAlignment = VerticalAlignment::Top;
myStackPanel2->Orientation = Orientation::Vertical;
Grid::SetColumn(myStackPanel2, 1);
Grid::SetRow(myStackPanel2, 0);
TextBlock^ myTextBlock7 = gcnew TextBlock();
myTextBlock7->FontSize = 18;
myTextBlock7->HorizontalAlignment = HorizontalAlignment::Center;
myTextBlock7->Margin = Thickness(0, 0, 0, 15);
myTextBlock7->Text = "StackPanel 2";
Button^ myButton4 = gcnew Button();
myButton4->Margin = Thickness(10, 0, 10, 0);
myButton4->Content = "Button 4";
Button^ myButton5 = gcnew Button();
myButton5->Margin = Thickness(10, 0, 10, 0);
myButton5->Content = "Button 5";
Button^ myButton6 = gcnew Button();
myButton6->Margin = Thickness(10, 0, 10, 0);
myButton6->Content = "Button 6";
TextBlock^ myTextBlock8 = gcnew TextBlock();
myTextBlock8->HorizontalAlignment = HorizontalAlignment::Center;
myTextBlock8->Text = "ColumnDefinition.Width = \"*\"";
TextBlock^ myTextBlock9 = gcnew TextBlock();
myTextBlock9->HorizontalAlignment = HorizontalAlignment::Center;
myTextBlock9->Text = "StackPanel.HorizontalAlignment = \"Stretch\"";
TextBlock^ myTextBlock10 = gcnew TextBlock();
myTextBlock10->HorizontalAlignment = HorizontalAlignment::Center;
myTextBlock10->Text = "StackPanel.VerticalAlignment = \"Top\"";
TextBlock^ myTextBlock11 = gcnew TextBlock();
myTextBlock11->HorizontalAlignment = HorizontalAlignment::Center;
myTextBlock11->Text = "StackPanel.Orientation = \"Horizontal\"";
TextBlock^ myTextBlock12 = gcnew TextBlock();
myTextBlock12->HorizontalAlignment = HorizontalAlignment::Center;
myTextBlock12->Text = "Button.Margin = \"10,0,10,0\"";
myStackPanel2->Children->Add(myTextBlock7);
myStackPanel2->Children->Add(myButton4);
myStackPanel2->Children->Add(myButton5);
myStackPanel2->Children->Add(myButton6);
myStackPanel2->Children->Add(myTextBlock8);
myStackPanel2->Children->Add(myTextBlock9);
myStackPanel2->Children->Add(myTextBlock10);
myStackPanel2->Children->Add(myTextBlock11);
myStackPanel2->Children->Add(myTextBlock12);

// Add the final child StackPanel.
StackPanel^ myStackPanel3 = gcnew StackPanel();
myStackPanel3->HorizontalAlignment = HorizontalAlignment::Left;
myStackPanel3->VerticalAlignment = VerticalAlignment::Top;
Grid::SetColumn(myStackPanel3, 2);
Grid::SetRow(myStackPanel3, 0);
TextBlock^ myTextBlock13 = gcnew TextBlock();
myTextBlock13->FontSize = 18;
myTextBlock13->HorizontalAlignment = HorizontalAlignment::Center;
myTextBlock13->Margin = Thickness(0, 0, 0, 15);
myTextBlock13->Text = "StackPanel 3";
Button^ myButton7 = gcnew Button();
myButton7->Margin = Thickness(10);
myButton7->Content = "Button 7";
Button^ myButton8 = gcnew Button();
myButton8->Margin = Thickness(10);
myButton8->Content = "Button 8";
Button^ myButton9 = gcnew Button();
myButton9->Margin = Thickness(10);
myButton9->Content = "Button 9";
TextBlock^ myTextBlock14 = gcnew TextBlock();
myTextBlock14->Text = "ColumnDefinition.Width = \"Auto\"";
TextBlock^ myTextBlock15 = gcnew TextBlock();
myTextBlock15->Text = "StackPanel.HorizontalAlignment = \"Left\"";
TextBlock^ myTextBlock16 = gcnew TextBlock();
myTextBlock16->Text = "StackPanel.VerticalAlignment = \"Top\"";
TextBlock^ myTextBlock17 = gcnew TextBlock();
myTextBlock17->Text = "StackPanel.Orientation = \"Vertical\"";
TextBlock^ myTextBlock18 = gcnew TextBlock();
myTextBlock18->Text = "Button.Margin = \"10\"";
myStackPanel3->Children->Add(myTextBlock13);
myStackPanel3->Children->Add(myButton7);
myStackPanel3->Children->Add(myButton8);
myStackPanel3->Children->Add(myButton9);
myStackPanel3->Children->Add(myTextBlock14);
myStackPanel3->Children->Add(myTextBlock15);
myStackPanel3->Children->Add(myTextBlock16);
myStackPanel3->Children->Add(myTextBlock17);
myStackPanel3->Children->Add(myTextBlock18);

// Add child content to the parent Grid.
myGrid->Children->Add(myStackPanel);
myGrid->Children->Add(myStackPanel2);
myGrid->Children->Add(myStackPanel3);

// Add the Grid as the lone child of the Border.
myBorder->Child = myGrid;

// Add the Border to the Window as Content and show the Window.
mainWindow->Content = myBorder;
mainWindow->Title = "Margin, Padding, and Alignment Sample";
mainWindow->Show();
<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" WindowTitle="Margins, Padding and Alignment Sample">
  <Border Background="LightBlue" 
          BorderBrush="Black" 
          BorderThickness="2" 
          CornerRadius="45" 
          Padding="25">
    <Grid Background="White" ShowGridLines="True">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
      </Grid.ColumnDefinitions>

    <StackPanel Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Name="StackPanel1" VerticalAlignment="Top">
        <TextBlock FontSize="18" HorizontalAlignment="Center" Margin="0,0,0,15">StackPanel1</TextBlock>
        <Button Margin="0,10,0,10">Button 1</Button>
        <Button Margin="0,10,0,10">Button 2</Button>
        <Button Margin="0,10,0,10">Button 3</Button>
        <TextBlock>ColumnDefinition.Width="Auto"</TextBlock>
        <TextBlock>StackPanel.HorizontalAlignment="Left"</TextBlock>
        <TextBlock>StackPanel.VerticalAlignment="Top"</TextBlock>
        <TextBlock>StackPanel.Orientation="Vertical"</TextBlock>
        <TextBlock>Button.Margin="0,10,0,10"</TextBlock>
    </StackPanel>

    <StackPanel Grid.Column="1" Grid.Row="0" HorizontalAlignment="Stretch" Name="StackPanel2" VerticalAlignment="Top" Orientation="Vertical">
        <TextBlock FontSize="18" HorizontalAlignment="Center" Margin="0,0,0,15">StackPanel2</TextBlock>
        <Button Margin="10,0,10,0">Button 4</Button>
        <Button Margin="10,0,10,0">Button 5</Button>
        <Button Margin="10,0,10,0">Button 6</Button>
        <TextBlock HorizontalAlignment="Center">ColumnDefinition.Width="*"</TextBlock>
        <TextBlock HorizontalAlignment="Center">StackPanel.HorizontalAlignment="Stretch"</TextBlock>
        <TextBlock HorizontalAlignment="Center">StackPanel.VerticalAlignment="Top"</TextBlock>
        <TextBlock HorizontalAlignment="Center">StackPanel.Orientation="Horizontal"</TextBlock>
        <TextBlock HorizontalAlignment="Center">Button.Margin="10,0,10,0"</TextBlock>
    </StackPanel>        

    <StackPanel Grid.Column="2" Grid.Row="0" HorizontalAlignment="Left" Name="StackPanel3" VerticalAlignment="Top">
        <TextBlock FontSize="18" HorizontalAlignment="Center" Margin="0,0,0,15">StackPanel3</TextBlock>
        <Button Margin="10">Button 7</Button>
        <Button Margin="10">Button 8</Button>
        <Button Margin="10">Button 9</Button>
        <TextBlock>ColumnDefinition.Width="Auto"</TextBlock>
        <TextBlock>StackPanel.HorizontalAlignment="Left"</TextBlock>
        <TextBlock>StackPanel.VerticalAlignment="Top"</TextBlock>
        <TextBlock>StackPanel.Orientation="Vertical"</TextBlock>
        <TextBlock>Button.Margin="10"</TextBlock>      
    </StackPanel>
  </Grid>
  </Border>    
</Page>

编译之后,前面的应用程序将生成类似于下图的 UI。 可以在元素之间的间距中一目了然地看到各个属性值的效果,并且各列中元素的重要属性值显示在 TextBlock 元素内。

一个应用程序中的几个定位属性

接下来的内容

利用 FrameworkElement 类定义的定位属性,可以精确地控制元素在 WPF 应用程序内的位置。 您现在已了解了多种方法,可以使用这些方法更好地通过 WPF 来定位元素。

还提供了一些附加资源,这些资源更加详细地介绍 WPF 布局。 面板概述 主题包含有关各个 Panel 元素的更多详细信息。 主题演练:开始使用 WPF介绍使用布局元素来定位组件并将它们的操作绑定到数据源的高级方法。

请参见

参考

FrameworkElement

HorizontalAlignment

VerticalAlignment

Margin

概念

面板概述

布局系统

其他资源

WPF Layout Gallery Sample