简体   繁体   English

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

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

I need to be able to fill a stackpanel with buttons but the buttons must appear at the bottom of the stackpanel first and populate upwards . 我需要能够用buttons填充stackpanel buttons但是这些按钮必须先出现在堆栈面板的底部,然后向上填充 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. 我已经尝试过垂直对齐,但无济于事。

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,而不是添加按钮。

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>

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. 然后,将附加的Dock属性设置为要添加到Bottom的每个按钮,然后再添加到DockPanel。

example : 例如:

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

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位置插入项目,而不是添加它们即可。

Try putting the StackPanel inside another container (not a StackPanel; maybe a DockPanel) and bottom-aligning it. 尝试将StackPanel放入另一个容器(不是StackPanel;也许是DockPanel)中,并使其底部对齐。 Then when you populate the buttons, put each new one into the first position. 然后,当您填充按钮时,将每个新按钮放到第一位置。

I found that using a UniformGrid with Column=1 gives a neat filling stack, or set Rows=1 give a neat horizonatally filled stack. 我发现使用Column = 1的UniformGrid可以得到整齐的填充堆栈,或者将Rows = 1可以得到整齐的水平填充堆栈。 And adding from the index 0 will work from bottom up. 并且从索引0开始添加将自下而上。

Love the transforms solution by Nir. 喜欢Nir的转换解决方案。 I was wondering if it could be done using transforms. 我想知道是否可以使用转换来完成。

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. 需要注意的是:不要在基于ScrollView的控件(如ListBox)上使用transforms技巧,因为滚动条操作将从内容中反转。 Hilarious to watch, as long as you're not the end user. 只要您不是最终用户,就很有趣。 ;> ;>

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

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