对齐堆栈面板中的项目? [英] Align items in a stack panel?

查看:16
本文介绍了对齐堆栈面板中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在水平方向的 StackPanel 中有 2 个控件,以便正确的项目应该停靠在 StackPanel 的右侧.

I was wondering if I can have 2 controls in a horizontal-oriented StackPanel so that the right item should be docked to the right side of the StackPanel.

我尝试了以下方法,但没有奏效:

I tried the following but it didn't work:

<StackPanel Orientation="Horizontal">
    <TextBlock>Left</TextBlock>
    <Button Width="30" HorizontalAlignment="Right">Right<Button>
</StackPanel>

在上面的代码段中,我希望 Button 停靠在 StackPanel 的右侧.

In the snippet above I want the Button to be docked to the right side of the StackPanel.

注意:我需要使用 StackPanel 而不是 Grid 等来完成.

Note: I need it to be done with StackPanel, not Grid etc.

推荐答案

您可以使用 DockPanel 来实现:

You can achieve this with a DockPanel:

<DockPanel Width="300">
    <TextBlock>Left</TextBlock>
    <Button HorizontalAlignment="Right">Right</Button>
</DockPanel>

区别在于 StackPanel 会将子元素排列成单行(垂直或水平),而 DockPanel 定义了一个区域,您可以在其中水平或垂直排列子元素,相对于彼此(Dock 属性更改元素相对于同一容器内其他元素的位置.对齐属性,例如 Horizo​​ntalAlignment,更改元素相对于其父元素的位置).

The difference is that a StackPanel will arrange child elements into single line (either vertical or horizontally) whereas a DockPanel defines an area where you can arrange child elements either horizontally or vertically, relative to each other (the Dock property changes the position of an element relative to other elements within the same container. Alignment properties, such as HorizontalAlignment, change the position of an element relative to its parent element).

更新

正如评论中指出的,您还可以使用 StackPanelFlowDirection 属性.请参阅@D_Bester 的答案.

As pointed out in the comments you can also use the FlowDirection property of a StackPanel. See @D_Bester's answer.

这篇关于对齐堆栈面板中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆