wpf 对齐堆栈面板中的项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2023201/
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
Align items in a stack panel?
提问by Shimmy Weitzhandler
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.
我想知道是否可以在水平方向的 StackPanel 中有 2 个控件,以便正确的项目应该停靠在 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.
在上面的代码片段中,我希望 Button 停靠在 StackPanel 的右侧。
Note: I need it to be done with StackPanel, not Grid etc.
注意:我需要使用 StackPanel 而不是 Grid 等来完成。
回答by Dirk Vollmar
You can achieve this with a DockPanel
:
您可以通过以下方式实现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).
区别在于 aStackPanel
将子元素排列成单行(垂直或水平),而 aDockPanel
定义了一个区域,您可以在其中水平或垂直排列子元素,相对于彼此(该Dock
属性改变元素相对于其他元素的位置)同一容器内的元素。对齐属性,例如HorizontalAlignment
,更改元素相对于其父元素的位置)。
Update
更新
As pointed out in the comments you can also use the FlowDirection
property of a StackPanel
. See @D_Bester's answer.
正如评论中指出的,您还可以使用 a 的FlowDirection
属性StackPanel
。请参阅@D_Bester 的回答。
回答by RusBog
Yo can set FlowDirection
of Stack panel
to RightToLeft
, and then all items will be aligned to the right side.
呦可以设置FlowDirection
的Stack panel
到RightToLeft
,然后所有项目将对准右侧。
回答by mopsled
For those who stumble upon this question, here's how to achieve this layout witha Grid
:
对于那些在这个问题谁绊倒,这里是如何做到这一点的布局与一Grid
:
<Grid>
<TextBlock Text="Server:"/>
<TextBlock Text="http://127.0.0.1" HorizontalAlignment="Right"/>
</Grid>
creates
创造
Server: http://127.0.0.1
回答by Ross
Could not get this working using a DockPanel quite the way I wanted and reversing the flow direction of a StackPanel is troublesome. Using a grid is not an option as items inside of it may be hidden at runtime and thus I do not know the total number of columns at design time. The best and simplest solution I could come up with is:
无法完全按照我想要的方式使用 DockPanel 进行此工作,并且反转 StackPanel 的流向很麻烦。使用网格不是一种选择,因为其中的项目可能在运行时隐藏,因此我不知道设计时的总列数。我能想到的最好和最简单的解决方案是:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="1" Orientation="Horizontal">
<!-- Right aligned controls go here -->
</StackPanel>
</Grid>
This will result in controls inside of the StackPanel being aligned to the right side of the available space regardless of the number of controls - both at design and runtime. Yay! :)
这将导致 StackPanel 内的控件与可用空间的右侧对齐,而不管控件的数量如何 - 无论是在设计还是运行时。好极了!:)
回答by D_Bester
This works perfectly for me. Just put the button first since you're starting on the right. If FlowDirection becomes a problem just add a StackPanel around it and specify FlowDirection="LeftToRight" for that portion. Or simply specify FlowDirection="LeftToRight" for the relevant control.
这对我来说非常有效。因为你从右边开始,所以先把按钮放在前面。如果 FlowDirection 成为一个问题,只需在它周围添加一个 StackPanel 并为该部分指定 FlowDirection="LeftToRight"。或者简单地为相关控件指定 FlowDirection="LeftToRight"。
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" FlowDirection="RightToLeft">
<Button Width="40" HorizontalAlignment="Right" Margin="3">Right</Button>
<TextBlock Margin="5">Left</TextBlock>
<StackPanel FlowDirection="LeftToRight">
<my:DatePicker Height="24" Name="DatePicker1" Width="113" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" />
</StackPanel>
<my:DatePicker FlowDirection="LeftToRight" Height="24" Name="DatePicker1" Width="113" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" />
</StackPanel>
回答by Attif
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Left" />
<Button Width="30" Grid.Column="1" >Right</Button>
</Grid>
回答by Ahmed.Net
for windows 10 use relativePanel instead of stack panel, and use
对于 Windows 10,使用 relativePanel 而不是堆栈面板,并使用
relativepanel.alignrightwithpanel="true"
relativepanel.alignrightwithpanel="true"
for the contained elements.
对于包含的元素。
回答by Christopher Zahrobsky
If you are having a problem like the one I had where labels were centered in my vertical stack panel, make sure you use full width controls. Delete the Width property, or put your button in a full-width container that allows internal alignment. WPF is all about using containers to control the layout.
如果您遇到了类似我在垂直堆栈面板中标签居中的问题,请确保使用全宽控件。删除 Width 属性,或将您的按钮放在允许内部对齐的全角容器中。WPF 就是使用容器来控制布局。
<StackPanel Orientation="Vertical">
<TextBlock>Left</TextBlock>
<DockPanel>
<Button HorizontalAlignment="Right">Right</Button>
</DockPanel>
</StackPanel>
Vertical StackPanel with Left Label followed by Right Button
I hope this helps.
我希望这有帮助。
回答by B. Clay Shannon
Maybe not what you want if you need to avoid hard-coding size values, but sometimes I use a "shim" (Separator) for this:
如果您需要避免硬编码大小值,可能不是您想要的,但有时我会为此使用“垫片”(分隔符):
<Separator Width="42"></Separator>