位置:首頁 > 軟件操作教程 > 編程開發(fā) > C# > 問題詳情

C# StackPanel 控件

提問人:劉團圓發(fā)布時間:2020-12-09

    可將StackPanel控件理解為精簡版的DockPanel,即子控件所??康倪吘壥枪潭ú蛔兊?。另一個差異是StackPanel中的最后一個子控件不會占滿剩余空間。不過,這些子控件默認情況下會拉伸到StackPanel控件的邊緣。

    控件的堆疊方向由三個屬性決定。Orientation可設(shè)置為Horizontal或Vertical, HorizontalAlignment和 VerticalAlignment可用于決定控件的堆梭是緊靠StackPanel的頂部、底部、左側(cè)還是右側(cè)進行排列。還可將對齊(Alignment)屬性的值設(shè)置為Center,讓控件在StackPanel的中間堆疊。

    下圖展示了兩個StackPanel控件,其中分別包含三個按鈕。上方的StackPanel控件的Orientation屬性設(shè)置為Horizontal,下方的StackPanel控件的Orientation屬性則設(shè)置為Vertical。

image.png

此處所用到的代碼如下所示(可在LayoutExamples\StackPanels.xaml下載文件中找到);

<Window x:Class = "LayoutExamples.StackPanels"

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

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

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/ITlarkup-compatibility/2006"

        xmlns:local="clr-namespace:LayoutExamples"

        mc:Ignorable="d"

        Title="StackPanels” Height="300" Width="300">

    <Grid>

        <StackPanel HorizontalAlignment="Left" Height="128" VerticalAlignment="Top"

Width="284" Orientation="Horizontai">

            <Button Content="Button" Height="128" VerticalAligmnent="Top" Width="75"/>

            <Button Content="Button" Height="128" VerticalAlignment="Top" Width="75"/>

            <Button Content="Button" Height="128" VerticalAlignment="Top" Width="75"/>

        </StackPanel>

        <StackPanel HorizontalAlignment="Left" Height="128" VerticalAlignment="Top"

Width="284" Margin="0,128,0,0,f Orientation="Vertical1^

            <Button Content="Button" HorizontalAlignment="Left" Width="284"/>

            <Button Content="Button" HorizontalAlignment="Left" Width="284"/>

            <Button Content="Button" HorizontalAlignment="Left" Width="284"/>

        </StackPanel>

    </Grid>

</Window>


繼續(xù)查找其他問題的答案?

相關(guān)視頻回答
回復(0)
返回頂部