WPF StackPanel 如何从下到上垂直填充?

发布于 2024-07-13 02:17:00 字数 149 浏览 8 评论 0原文

我需要能够用按钮填充stackpanel,但按钮必须首先出现在stackpanel的底部并向上填充。 这些按钮是动态创建的,并且数量未知,因此视觉黑客是行不通的。 我尝试过垂直对齐,但没有成功。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

叹倦 2024-07-20 02:17:00

像这样:

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

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

Like so:

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

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

π浅易 2024-07-20 02:17:00

或者,您可以将 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>

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>
    <!-- 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>
花落人断肠 2024-07-20 02:17:00

另一种选择是使用 DockPanel。

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

然后,在添加到 DockPanel 之前,为要添加到 Bottom 的每个按钮设置附加的 Dock 属性。

例子 :

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

Another alternative is to use a DockPanel instead.

Just set the LastChildFill to false on the DockPanel.

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

example :

            var button = new Button();
            DockPanel.SetDock(button, Dock.Bottom);
尽揽少女心 2024-07-20 02:17:00

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

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

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

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

    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.

紫罗兰の梦幻 2024-07-20 02:17:00

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

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.

月下伊人醉 2024-07-20 02:17:00

我发现使用 Column=1 的 UniformGrid 可以提供整齐的填充堆栈,或者设置 Rows=1 可以提供整齐的水平填充堆栈。 从索引 0 开始添加将从下往上进行。

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.

浪菊怪哟 2024-07-20 02:17:00

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

但需要注意的是:不要在基于 ScrollView 的控件(例如 ListBox)上使用变换技巧,因为滚动条操作将与内容相反。 只要您不是最终用户,观看起来就会很搞笑。 ;>

Love the transforms solution by 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. Hilarious to watch, as long as you're not the end user. ;>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文