WPF布局控件之StackPanel
StackPanel
Stack,英文意思是堆栈,StackPanel
,意思是堆栈式布局,相当于把控件给堆起来。如果不设置StackPanel
中控件的宽高,那么其中控件的宽高是默认和StackPanel
一样的,如果设置控件宽高,那么控件还是占一行,只是说控件本身宽高改变。如下:
<Window x:Class="LearnLayout.StackPanelWin"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/markup-compatibility/2006"xmlns:local="clr-namespace:LearnLayout"mc:Ignorable="d"Title="StackPanelWin" Height="450" Width="800"><StackPanel><Button Content="牛马人的一生" Background="lavender" Width="200"/><Button Content="我不是牛马" Background="Pink"/><Button Content="下辈子还当牛马" Background="Snow"/></StackPanel>
</Window>
StackPanel
默认是竖向(垂直,Orientation="Vertical"
)堆起来的,如上所示,如果想要横向(水平),可以通过Orientation="Horizontal"
来设置。如下:
<Window x:Class="LearnLayout.StackPanelWin"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/markup-compatibility/2006"xmlns:local="clr-namespace:LearnLayout"mc:Ignorable="d"Title="StackPanelWin" Height="450" Width="800"><StackPanel Orientation="Horizontal"><Button Content="牛马人的一生" Background="lavender" Width="200"/><Button Content="我不是牛马" Background="Pink"/><Button Content="下辈子还当牛马" Background="Snow"/></StackPanel>
</Window>