简体   繁体   English

WPF StackPanel垂直框架全高

[英]WPF StackPanel vertical Frame full height

I need to fill empty space with last element in stackpanel (Frame): 我需要用stackpanel(框架)中的最后一个元素填充空白空间:

    <DockPanel>
        <StackPanel Orientation="Vertical">
            <Frame NavigationUIVisibility="Hidden" Height="100" x:Name="_menu" />
            <Frame NavigationUIVisibility="Hidden" x:Name="_mainFrame" />
        </StackPanel>
    </DockPanel>

_mainFrame - fill empty space from _menu Frame to the bottom _mainFrame-从_menu框架到底部填充空白空间

StackPanel is not the layout container for allowing elements to "fill remaining space". StackPanel不是用于允许元素“填充剩余空间”的布局容器。 StackPanel has a specific purpose among other layout containers. 在其他布局容器中, StackPanel具有特定的用途。 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. 这将使孩子们得到他们想要的空间。

For your requirement, you'd want to go with either a DockPanel / Grid . 根据您的要求,您可以使用DockPanel / Grid Both of these controls would allow the parent container to "limit" it's children's dimension based on what's available. 这两个控件都将允许父容器根据可用容器来“限制”其子维度。

In your example you already have a DockPanel . 在您的示例中,您已经有一个DockPanel However you've put a StackPanel as it's child and it's the only child which doesn't make much sense. 但是,您已经将StackPanel作为它的孩子,这是唯一没有太大意义的孩子。 You want to try and use a layout container when you have more than 1 child item to put into a layout. 当要放入一个布局中的子项超过1个时,您想尝试使用布局容器。 In this case the StackPanel is the only child of the DockPanel . 在这种情况下,StackPanel是DockPanel的唯一子级。

To get your requirement by using a DockPanel , you could go with, 为了使用DockPanel满足您的要求,您可以使用,

<DockPanel>
    <Frame NavigationUIVisibility="Hidden" Height="100" x:Name="_menu" DockPanel.Dock="Top" />
    <Frame NavigationUIVisibility="Hidden" x:Name="_mainFrame" />
</DockPanel>

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. 默认情况下, DockPanel使用LastChildFill="True"因此声明中的第二项将被拉伸以填充您需要的剩余可用空间。 If you set this property to false, it wouldn't stretch it. 如果将此属性设置为false,则不会扩展它。

To do the same thing with a Grid : 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>

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. 根据您的要求,我更喜欢使用DockPanelGrid方法只是一个很好的认识,并且可以在情况需要时使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM