次の方法で共有


方法: StackPanel を作成する

この例では、StackPanelを作成する方法を示します。

StackPanel を使用すると、指定した方向に要素をスタックできます。 StackPanelで定義されているプロパティを使用すると、コンテンツを垂直方向 (既定の設定) または水平方向の両方に流すことができます。

次の例では、StackPanelを使用して、それぞれ異なる BorderBackgroundを持つ 5 つの TextBlock コントロールを垂直方向にスタックします。 指定された 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>  

関連項目