默认情况下,StackPanel 根据下面的逻辑来设置子控件的大小:
如果是Orientation 是垂直的:
-子控件的高度将根据其自身的需要确定
-子控件的宽度将横向占满整个StackPanel
如果Orientation 是水平的:
-子控件的宽度将根据其自身的需要确定
-子控件的高度将横向占满整个StackPanel
下面的地址将StackPanel 的子控件垂直排列,显示结果如图。
<StackPanel>
<Label Content="Gene Autry the singing cowboy" Background="Pink"/>
<Button Content="I Like Gene" FontSize="18"/>
<Label Content="Roy Rogers" Background="Aqua" FontSize="20"/>
<Button Content="I Like Roy Rogers Yes I Do"/>
<TextBox Text="This is a TextBox"/>
<Label Content="Tex Ritter" Background="PaleGoldenrod"/>
<Button Content="I Like Tex" FontSize="20" />
<Label Content="Jorge Negrete" Background="Lavender"/>
<Button Content="I Like Jorge Yes I Do"/>
</StackPanel>
如果水平排列,结果将会如下:
原文地址:https://wpf.2000things.com/2011/06/14/320-default-size-of-stackpanel-child-controls/