Canvas、StackPanel、WrapPanel、DockPanel和Grid是WPF中主要的5种内建面板,这些面板类都位于System.Windows.Controls命名空间下。
StackPanel是一个受欢迎的面板,因为它方便好用,它会顺序对它的子元素进行排列。它是少数几个没有定义任何附加属性的面板之一。由于没有附加属性来排列子元素,只有一种方法可以定制StackPanel的行为——设置Orientation属性为Horizontal或Vertical,Vertical是默认值。
<Window x:Class="WPF5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<StackPanel Width="100">
<Button Background="Red">NoScaling</Button>
<Button Background="Orange">
<Button.RenderTransform>
<ScaleTransform ScaleX="2"></ScaleTransform>
</Button.RenderTransform>
X
</Button>
<Button Background="Yellow">
<Button.RenderTransform>
<ScaleTransform ScaleX="2" ScaleY="2"></ScaleTransform>
</Button.RenderTransform>
X+Y
</Button>
<Button Background="Lime">
<Button.RenderTransform>
<ScaleTransform ScaleY="2"></ScaleTransform>
</Button.RenderTransform>
Y
</Button>
</StackPanel>
</StackPanel>
</Window>
效果如下图所示:
当把StackPanel的FlowDirection属性设置为RightToLeft,Orientation属性设置为Horizontal,StackPanel将从右向左排列元素。