此示例演示如何创建一个 StackPanel。
示例:
使用 A StackPanel ,可以按指定方向堆叠元素。 通过使用定义的 StackPanel属性,内容可以垂直流动,这是默认设置,也可以水平流动。
以下示例使用TextBlock垂直堆叠五个Border控件,每个控件具有不同的Background和StackPanel。 没有指定 Width 的子元素会拉伸以填充父窗口;而指定了 Width 的子元素则在窗口内居中显示。
默认堆栈方向为垂直StackPanel。 要控制StackPanel中的内容流,请使用Orientation属性。 可以使用属性 HorizontalAlignment 控制水平对齐方式。
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" WindowTitle="StackPanel Sample">
<StackPanel>
<Border Background="SkyBlue" BorderBrush="Black" BorderThickness="1">
<TextBlock Foreground="Black" FontSize="12">Stacked Item #1</TextBlock>
</Border>
<Border Width="400" Background="CadetBlue" BorderBrush="Black" BorderThickness="1">
<TextBlock Foreground="Black" FontSize="14">Stacked Item #2</TextBlock>
</Border>
<Border Background="LightGoldenRodYellow" BorderBrush="Black" BorderThickness="1">
<TextBlock Foreground="Black" FontSize="16">Stacked Item #3</TextBlock>
</Border>
<Border Width="200" Background="PaleGreen" BorderBrush="Black" BorderThickness="1">
<TextBlock Foreground="Black" FontSize="18">Stacked Item #4</TextBlock>
</Border>
<Border Background="White" BorderBrush="Black" BorderThickness="1">
<TextBlock Foreground="Black" FontSize="20">Stacked Item #5</TextBlock>
</Border>
</StackPanel>
</Page>