wpf 贴在 StackPanel 的底部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29698851/
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 13:05:35  来源:igfitidea点击:

Stick to the bottom of a StackPanel

wpf

提问by peter

In my window, there is a Grid that takes up 100% of the available vertical space. Inside that Grid is a StackPanel with another StackPanel and a Frame inside. I would like to have that Frame stick to the bottom of the parent StackPanel.

在我的窗口中,有一个 Grid 占据了 100% 的可用垂直空间。在那个 Grid 里面是一个 StackPanel,里面有另一个 StackPanel 和一个 Frame。我想让那个 Frame 粘在父 StackPanel 的底部。

<StackPanel>
    <StackPanel>
       //normal content
     </StackPanel>
     <Frame /> // should stick to the bottom
</StackPanel>

So that, for example, when the user resizes the Window and the Grid gains height, the Frame inside sticks to the bottom.

因此,例如,当用户调整 Window 的大小并且 Grid 增加高度时,内部的 Frame 会粘在底部。

I have tried various things, using VerticalAlign="Stretch"or a DockPanel and assigning the Frame a DockPanel.Dock="Bottom", but to no success. I'd be thankful for a hint or two. I don't need the StackPanel to be a StackPanel, it can also be a DockPanel.

我尝试了各种方法,使用VerticalAlign="Stretch"或 DockPanel 并分配 Frame a DockPanel.Dock="Bottom",但没有成功。我会感谢一两个提示。我不需要 StackPanel 是 StackPanel,它也可以是 DockPanel。

回答by Sheridan

A StackPanelis not the correct Panelto use for your requirements as they do not provide the same layout and resizing capabilities as a Grid. Therefore, simply replace it with a Grid, give the majority of the space to the inner StackPaneland then the Framewill stick to the bottom.

AStackPanelPanel适合用于您的要求,因为它们不提供与Grid. 因此,只需将其替换为Grid,将大部分空间分配给内部StackPanel,然后Frame将其粘在底部。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />   
    </Grid.RowDefinitions>
    <StackPanel>
       //normal content
     </StackPanel>
     <Frame Grid.Row="1" />
</Grid>

You can find out more about the different WPF Panels in the Panels Overviewpage on MSDN.

您可以在 MSDN 上Panel面板概述页面中找到有关不同 WPF的更多信息。

回答by Manish Basantani

Looks more like a use case for DockPanel

看起来更像是一个用例 DockPanel

<DockPanel>
    <StackPanel>
        <!--//normal content-->
    </StackPanel>
    <Frame DockPanel.Dock="Bottom"/> <!--// should stick to the bottom-->
</DockPanel>

回答by sohaiby

According to MSDN docs about StackPanel

根据关于 StackPanel 的 MSDN 文档

A StackPanel Arranges child elements into a single line that can be oriented horizontally or vertically.

StackPanel 将子元素排列成可以水平或垂直定向的单行。

So in your case, StackPanel is not the right control to use. If you want your control to be fixed at a particular position, you can use DockPaneland place your control inside it.

因此,在您的情况下, StackPanel 不是正确的控件。如果您希望您的控件固定在特定位置,您可以使用DockPanel并将您的控件放在其中。

<DockPanel>
        <Frame DockPanel.Dock="Bottom"/>
</DockPanel>