WPF StackPanel
图1.1
Introduction介绍
The StackPanel
in WPF is a simple and useful layout panel. It stacks its child elements below or beside each other, dependening on its orientation. This is very useful to create any kinds of lists. All WPF ItemsControls
like ComboBox
, ListBox
or Menu
use a StackPanel as their internal layout panel.
WPF中的StackPanel控件是一种简单常用的布局控件。它可根据orientation属性设置面板里面的每个子元素是依靠在前一个控件的旁边(横向)还是下面(纵向)。对于创建各种类型的列表非常有用。所有的继承于ItemsControls的WPF控件,如ComboBox,ListBox和Menu控件都可以使用StackPanel做为它的内部布局面板。
<StackPanel>
<TextBlock Margin="10" FontSize="20">How do you like your coffee?</TextBlock>
<Button Margin="10">Black</Button>
<Button Margin="10">With milk</Button>
<Button Margin="10">Latte machiato</Button>
<Button Margin="10">Chappuchino</Button>
</StackPanel>
上面这一段代码将把StackPanel面板里面的每个控件,从上往下排列,如图1.1所示。
Stack Items horizontally
A good example for a horizontal stack panel are the "OK" and "Cancel" buttons of a dialog window. Because the size of the text can change if the user changes the font-size or switches the language we should avoid fixed sized buttons. The stack panel aligns the two buttons depending on their desired size. If they need more space they will get it automatically. Never mess again with too small or too large buttons.
有一个很好的例子,如有一个有“OK"和"Cancel“按钮的对话框,因为按钮上的文字可能因字体的改变而发生大小改变,我们应该避免固定按钮大小的写法。StackPanel会自动根据面板的大小的自动调整内部控件的大小。我们就不用为按钮太大或太小而烦恼了。
<StackPanel Margin="8" Orientation="Horizontal"> <Button MinWidth="93">OK</Button> <Button MinWidth="93" Margin="10,0,0,0">Cancel</Button> </StackPanel>
2010-04-16