WPF StackPanel垂直框架全高 [英] WPF StackPanel vertical Frame full height
问题描述
我需要用stackpanel(框架)中的最后一个元素填充空白空间:
I need to fill empty space with last element in stackpanel (Frame):
<DockPanel>
<StackPanel Orientation="Vertical">
<Frame NavigationUIVisibility="Hidden" Height="100" x:Name="_menu" />
<Frame NavigationUIVisibility="Hidden" x:Name="_mainFrame" />
</StackPanel>
</DockPanel>
_mainFrame-从_menu Frame到底部填充空白
_mainFrame - fill empty space from _menu Frame to the bottom
推荐答案
StackPanel
不是用于允许元素填充剩余空间"的布局容器. StackPanel
在其他布局容器中具有特定目的.它是垂直/水平地堆叠"物品,并根据孩子的期望高度(比可用物品高)进行工作.它将使孩子们得到他们想要的空间.
StackPanel
is not the layout container for allowing elements to "fill remaining space". StackPanel
has a specific purpose among other layout containers. it's to "Stack" items vertically/horizontally and works based on it's children's desired height than what's available. It will allow it's children to get how much ever space they desire.
根据您的要求,您可以选择DockPanel
/Grid
.这两个控件都将允许父容器根据可用容器来限制"其子容器的尺寸.
For your requirement, you'd want to go with either a DockPanel
/Grid
. Both of these controls would allow the parent container to "limit" it's children's dimension based on what's available.
在您的示例中,您已经有一个DockPanel
.但是,您已将StackPanel
作为它的子项,并且它是唯一没有太大意义的子项.当要放入一个布局中的子项目超过1个时,您要尝试使用布局容器.在这种情况下,StackPanel是DockPanel
的唯一子代.
In your example you already have a DockPanel
. However you've put a StackPanel
as it's child and it's the only child which doesn't make much sense. You want to try and use a layout container when you have more than 1 child item to put into a layout. In this case the StackPanel is the only child of the DockPanel
.
要使用DockPanel
满足您的要求,可以选择
To get your requirement by using a DockPanel
, you could go with,
<DockPanel>
<Frame NavigationUIVisibility="Hidden" Height="100" x:Name="_menu" DockPanel.Dock="Top" />
<Frame NavigationUIVisibility="Hidden" x:Name="_mainFrame" />
</DockPanel>
DockPanel
默认情况下使用LastChildFill="True"
,因此声明中的第二项将被拉伸以填充剩余的可用空间,这是您需要的.如果将此属性设置为false,则不会拉伸它.
DockPanel
by default uses LastChildFill="True"
thus the second item in the declaration will be stretched to fill remaining space available which is what you need. If you set this property to false, it wouldn't stretch it.
要使用Grid
做同样的事情:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Frame NavigationUIVisibility="Hidden" Height="100" x:Name="_menu" />
<Frame NavigationUIVisibility="Hidden" x:Name="_mainFrame" Grid.Row="1" />
</Grid>
根据您的要求,我更喜欢使用DockPanel
,Grid
方法只是需要注意的一点,并且可能在情况需要时使用.
for your requirement, I'd prefer to go with the DockPanel
, the Grid
method is just something good to be aware about and probably use when the situation demands it.
这篇关于WPF StackPanel垂直框架全高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!