WPF StackPanel 垂直框架全高

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18568567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:33:36  来源:igfitidea点击:

WPF StackPanel vertical Frame full height

wpfheightframestackpanel

提问by Nikalaj

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 - fill empty space from _menu Frame to the bottom

_mainFrame - 填充从 _menu Frame 到底部的空白空间

采纳答案by Viv

StackPanelis not the layout container for allowing elements to "fill remaining space". StackPanelhas 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.

StackPanel不是允许元素“填充剩余空间”的布局容器。StackPanel在其他布局容器中具有特定用途。它是垂直/水平地“堆叠”项目,并根据孩子们想要的高度而不是可用的高度来工作。它将允许孩子们获得他们想要的空间。

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/ Grid。这两个控件都允许父容器根据可用内容“限制”其子容器的维度。

In your example you already have a DockPanel. However you've put a StackPanelas 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. 但是,您已经将 aStackPanel作为它的孩子,并且它是唯一没有多大意义的孩子。当您将多个子项放入布局时,您想尝试使用布局容器。在这种情况下,StackPanel 是DockPanel.

To get your requirement by using a DockPanel, you could go with,

要通过使用 a 来满足您的要求DockPanel,您可以使用,

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

DockPanelby 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.

DockPanel默认情况下使用,LastChildFill="True"因此声明中的第二项将被拉伸以填充剩余的可用空间,这正是您需要的。如果将此属性设置为 false,则不会拉伸它。

To do the same thing with a Grid:

用 a 做同样的事情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 Gridmethod is just something good to be aware about and probably use when the situation demands it.

对于您的要求,我更愿意使用DockPanel,该Grid方法只是需要注意并可能在情况需要时使用的好方法。