WPF StackPanel 如何从底部到顶部垂直填充?

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

How can a WPF StackPanel fill vertically from bottom to top?

wpfstackpanel

提问by EightyOne Unite

I need to be able to fill a stackpanelwith buttonsbut the buttons must appear at the bottom of the stackpanel first and populate upwards. The buttons are created dynamically and there's an unknown number of them so visual hackery just won't work. I've tried experimenting with vertical alignments but to no avail.

我需要能够填补stackpanelbuttons但按键必须在StackPanel的底部先出现并填充向上。这些按钮是动态创建的,并且它们的数量未知,因此视觉黑客是行不通的。我试过尝试垂直对齐,但无济于事。

回答by Pop Catalin

Like so:

像这样:

<StackPanel VerticalAlignment="Bottom">
    ...
</StackPanel>

and to populate with buttons upward you must insert the buttons at position 0, instead of adding them.

并且要向上填充按钮,您必须在位置 0 插入按钮,而不是添加它们。

回答by Nir

Or you can rotate the StackPanel 180 degrees to get the buttons to stack from the bottom to the top and then rotating the buttons another 180 degrees to get them right-side-up again:

或者,您可以将 StackPanel 旋转 180 度以使按钮从底部到顶部堆叠,然后将按钮再旋转 180 度以使它们再次正面朝上:

<StackPanel>
    <!-- rotate all buttons inside this panel -->
    <StackPanel.Resources>
        <Style TargetType="Button">
            <Setter Property="LayoutTransform">
                <Setter.Value>
                    <RotateTransform Angle="180"/>
                </Setter.Value>
            </Setter>
        </Style>
    </StackPanel.Resources>

    <!-- rotate the stack panel -->
    <StackPanel.LayoutTransform>
       <RotateTransform Angle="180"/>
    </StackPanel.LayoutTransform>

    <!-- content -->
    <Button>1</Button>
    <Button>2</Button>

</StackPanel>

回答by JB.

Another alternative is to use a DockPanel instead.

另一种替代方法是使用 DockPanel。

Just set the LastChildFill to false on the DockPanel.

只需在 DockPanel 上将 LastChildFill 设置为 false。

Then set the attached Dock property to each button you are adding to Bottom before adding to the DockPanel.

然后在添加到 DockPanel 之前,将附加的 Dock 属性设置为您添加到底部的每个按钮。

example :

例子 :

            var button = new Button();
            DockPanel.SetDock(button, Dock.Bottom);

回答by Oleg

The best way to solve the problem is to implement custom container derived from stackpanel but quick and dirty solution if elements are added at runtime is

解决问题的最佳方法是实现从 stackpanel 派生的自定义容器,但如果在运行时添加元素,则快速而肮脏的解决方案是

    public Window1()
    {
        InitializeComponent();
        for (int i = 0; i < 10; i++)
        {
            Button btn = new Button();
            btn.Content = "Button " + i;
            MyStack.Children.Insert(0, btn);
        }
    }

Just insert item at 0 position instead of adding them.

只需在 0 位置插入项目而不是添加它们。

回答by Ria

Try putting the StackPanel inside another container (not a StackPanel; maybe a DockPanel) and bottom-aligning it. Then when you populate the buttons, put each new one into the first position.

尝试将 StackPanel 放在另一个容器(不是 StackPanel;可能是 DockPanel)中并使其底部对齐。然后,当您填充按钮时,将每个新按钮放在第一个位置。

回答by Tri Q Tran

I found that using a UniformGrid with Column=1 gives a neat filling stack, or set Rows=1 give a neat horizonatally filled stack. And adding from the index 0 will work from bottom up.

我发现使用带有 Column=1 的 UniformGrid 会得到一个整齐的填充堆栈,或者设置 Rows=1 会得到一个整齐的水平填充堆栈。从索引 0 添加将自下而上工作。

回答by dthorpe

Love the transforms solution by Nir. I was wondering if it could be done using transforms.

喜欢 Nir ​​的转换解决方案。我想知道是否可以使用转换来完成。

One caveat, though: Don't use the transforms trick on a ScrollView based control such as a ListBox because the scroll bar operation will be inverted from the content. Hilarious to watch, as long as you're not the end user. ;>

但有一个警告:不要在基于 ScrollView 的控件(例如 ListBox)上使用转换技巧,因为滚动条操作将与内容相反。只要您不是最终用户,就可以看热闹。;>