对齐堆叠面板中的项目?(Align items in a stack panel?)

编程入门 行业动态 更新时间:2024-10-28 18:30:51
对齐堆叠面板中的项目?(Align items in a stack panel?)

我想知道如果我可以在一个水平方向的StackPanel中有两个控件,以便正确的项目应该停靠在StackPanel的右侧。

我试过以下,但它没有工作:

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

在上面的代码段中,我想要将Button停靠在StackPanel的右侧。

注意:我需要使用StackPanel,而不是Grid等。

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>

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

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

最满意答案

您可以使用DockPanel实现此目的:

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

不同之处在于, StackPanel将将子元素StackPanel单行 (垂直或水平),而DockPanel定义了一个区域,您可以相对于彼此水平或垂直排列子元素 ( Dock属性更改元素的位置相对于同一容器内的其他元素,对齐属性(如HorizontalAlignment )更改元素相对于其父元素的位置)。

You can achieve this with a DockPanel:

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

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

Update

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

更多推荐

本文发布于:2023-04-27 15:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327328.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:面板   项目   Align   panel   stack

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!