Align items in a stack panel?
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.
Yo can set FlowDirection
of Stack panel
to RightToLeft
, and then all items will be aligned to the right side.
For those who stumble upon this question, here's how to achieve this layout with a Grid
:
<Grid> <TextBlock Text="Server:"/> <TextBlock Text="http://127.0.0.1" HorizontalAlignment="Right"/></Grid>
creates
Server: http://127.0.0.1