简体   繁体   English

WPF StackPanel - 如何将物品从底部推出

[英]WPF StackPanel - how to push items off bottom

I have a StackPanel spanning the entire screen vertically in a grid, and the panel's VerticalAlignment option set to Bottom.我有一个 StackPanel 在网格中垂直跨越整个屏幕,并且面板的 VerticalAlignment 选项设置为底部。 When I add items to the panel in code, their positions start at the bottom, and move up when I add more.当我在代码中将项目添加到面板时,它们的位置从底部开始,当我添加更多时它们会向上移动。 This is desired.这是所希望的。 However, what is not desired is that after the screen is filled vertically with items, adding more pushes them off the top of the screen.然而,不希望的是,在屏幕垂直填充项目后,添加更多项目会将它们推离屏幕顶部。 My desire is to have additional items fall off the bottom;我的愿望是让额外的物品从底部掉下来; I don't care if they are not visible;我不在乎它们是否不可见; I just don't want the topmost items to disappear.我只是不希望最上面的项目消失。

Any tricks you might know of to help me through this?您可能知道什么技巧可以帮助我解决这个问题? Thank you in advance.先感谢您。

My desire is to have additional items fall off the bottom我的愿望是让额外的物品从底部掉下来

You need to wrap the StackPanel in another container that allows the StackPanel itself to grow as much as it wants, in a way that makes the StackPanel appear to expand from the bottom, but in reality expand from the top so that when it exceeds available space, the excess is removed from the bottom instead of the top.您需要将StackPanel包装在另一个容器中,以使StackPanel本身可以随心所欲地增长,以使StackPanel看起来从底部扩展,但实际上是从顶部扩展,这样当它超过可用空间时,多余的部分从底部而不是顶部移除。

The Grid control will do exactly that. Grid控件正是这样做的。 For example:例如:

<Window x:Class="TestSO67169225StackPanelFromBottom.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
  <Window.Resources>
    <Style TargetType="TextBlock">
      <Setter Property="FontSize" Value="36"/>
    </Style>
  </Window.Resources>
  
  <Grid VerticalAlignment="Bottom">
    <StackPanel>
      <TextBlock Text="StackPanel Item 1"/>
      <TextBlock Text="StackPanel Item 2"/>
      <TextBlock Text="StackPanel Item 3"/>
      <TextBlock Text="StackPanel Item 4"/>
      <TextBlock Text="StackPanel Item 5"/>
      <TextBlock Text="StackPanel Item 6"/>
    </StackPanel>
  </Grid>
</Window>

Looks like this:看起来像这样:

未满 StackPanel 从底部增长

But when you add more items, looks like this:但是当您添加更多项目时,看起来像这样:

StackPanel 溢出的项目从底部溢出

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

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