[WPF]StackPanel

Introduction

StackPanel容器可讓容器內的控制項呈現水平或垂直排列。

命名空間

System.Windows.Controls

XMLNS

http://schemas.microsoft.com/winfx/xaml/presentation

Assemble

PresentationFramework (在 PresentationFramework.dll)

功能

將子項目排列在可為水平或垂直方向的單一行中。

範例一

<Window x:Class=”Window1”


    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation


    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml


    Title=”Window1” Height=”300” Width=”300”>


    <StackPanel Orientation=”Horizontal” >


        <Button Background=”Wheat” >按鈕一</Button>


        <Button Background=”Yellow” >按鈕二</Button>


        <Button Background=”#00AA00”>按鈕三</Button>


    </StackPanel>


</Window>






執行結果



image





範例二


<Window x:Class=“Window1”

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel Orientation="Vertical"  >
        <Button Background="Wheat" >按鈕一</Button>
        <Button Background="Yellow" >按鈕二</Button>
        <Button Background="#00AA00">按鈕三</Button>
    </StackPanel>
</Window>

執行結果

image

範例三

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" WindowTitle="StackPanel Sample">
<pre>    <span class="kwrd">&lt;</span><span class="html">StackPanel</span><span class="kwrd">&gt;</span></pre>

<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">Border</span> <span class="attr">Background</span><span class="kwrd">="SkyBlue"</span> <span class="attr">BorderBrush</span><span class="kwrd">="Black"</span> <span class="attr">BorderThickness</span><span class="kwrd">="1"</span><span class="kwrd">&gt;</span></pre>

<pre>            <span class="kwrd">&lt;</span><span class="html">TextBlock</span> <span class="attr">Foreground</span><span class="kwrd">="Black"</span> <span class="attr">FontSize</span><span class="kwrd">="12"</span><span class="kwrd">&gt;</span>Stacked Item #1<span class="kwrd">&lt;/</span><span class="html">TextBlock</span><span class="kwrd">&gt;</span></pre>

<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">Border</span><span class="kwrd">&gt;</span></pre>

<pre>        <span class="kwrd">&lt;</span><span class="html">Border</span> <span class="attr">Width</span><span class="kwrd">="400"</span> <span class="attr">Background</span><span class="kwrd">="CadetBlue"</span> <span class="attr">BorderBrush</span><span class="kwrd">="Black"</span> <span class="attr">BorderThickness</span><span class="kwrd">="1"</span><span class="kwrd">&gt;</span></pre>

<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">TextBlock</span> <span class="attr">Foreground</span><span class="kwrd">="Black"</span> <span class="attr">FontSize</span><span class="kwrd">="14"</span><span class="kwrd">&gt;</span>Stacked Item #2<span class="kwrd">&lt;/</span><span class="html">TextBlock</span><span class="kwrd">&gt;</span></pre>

<pre>        <span class="kwrd">&lt;/</span><span class="html">Border</span><span class="kwrd">&gt;</span></pre>

<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">Border</span> <span class="attr">Background</span><span class="kwrd">="LightGoldenRodYellow"</span> <span class="attr">BorderBrush</span><span class="kwrd">="Black"</span> <span class="attr">BorderThickness</span><span class="kwrd">="1"</span><span class="kwrd">&gt;</span></pre>

<pre>            <span class="kwrd">&lt;</span><span class="html">TextBlock</span> <span class="attr">Foreground</span><span class="kwrd">="Black"</span> <span class="attr">FontSize</span><span class="kwrd">="16"</span><span class="kwrd">&gt;</span>Stacked Item #3<span class="kwrd">&lt;/</span><span class="html">TextBlock</span><span class="kwrd">&gt;</span></pre>

<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">Border</span><span class="kwrd">&gt;</span></pre>

<pre>        <span class="kwrd">&lt;</span><span class="html">Border</span> <span class="attr">Width</span><span class="kwrd">="200"</span> <span class="attr">Background</span><span class="kwrd">="PaleGreen"</span> <span class="attr">BorderBrush</span><span class="kwrd">="Black"</span> <span class="attr">BorderThickness</span><span class="kwrd">="1"</span><span class="kwrd">&gt;</span></pre>

<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">TextBlock</span> <span class="attr">Foreground</span><span class="kwrd">="Black"</span> <span class="attr">FontSize</span><span class="kwrd">="18"</span><span class="kwrd">&gt;</span>Stacked Item #4<span class="kwrd">&lt;/</span><span class="html">TextBlock</span><span class="kwrd">&gt;</span></pre>

<pre>        <span class="kwrd">&lt;/</span><span class="html">Border</span><span class="kwrd">&gt;</span></pre>

<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">Border</span> <span class="attr">Background</span><span class="kwrd">="White"</span> <span class="attr">BorderBrush</span><span class="kwrd">="Black"</span> <span class="attr">BorderThickness</span><span class="kwrd">="1"</span><span class="kwrd">&gt;</span></pre>

<pre>            <span class="kwrd">&lt;</span><span class="html">TextBlock</span> <span class="attr">Foreground</span><span class="kwrd">="Black"</span> <span class="attr">FontSize</span><span class="kwrd">="20"</span><span class="kwrd">&gt;</span>Stacked Item #5<span class="kwrd">&lt;/</span><span class="html">TextBlock</span><span class="kwrd">&gt;</span></pre>

<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">Border</span><span class="kwrd">&gt;</span></pre>

<pre>    <span class="kwrd">&lt;/</span><span class="html">StackPanel</span><span class="kwrd">&gt;</span></pre>

<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">Page</span><span class="kwrd">&gt;</span></pre>


執行結果

image