How can I insert elements into a stackpanel, and they start positioned by center? Something like this:
|_ _ x _ _ |
|_ x x _ _ |
|_ x x x _ |
The "x" are the elements, and the "_" are blank space.
Is there something already implemented?
StackPanel is a layout panel that arranges child elements into a single line that can be oriented horizontally or vertically. By default, StackPanel stacks items vertically from top to bottom in the order they are declared. You can set the Orientation property to Horizontal to stack items from left to right.
How do I center text in a label in WPF? If you want to center each line, use a TextBlock instead, and set TextAlignment="Center" .
Example. A StackPanel allows you to stack elements in a specified direction. By using properties that are defined on StackPanel, content can flow both vertically, which is the default setting, or horizontally.
You could wrap your elements in a Grid
:
<Grid Width="720">
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal" Width="Auto" >
<!-- elements go here -->
</StackPanel>
</Grid>
This is the simplest approach in my opinion. You also need to make sure that the StackPanel's Width="Auto" and Grid's Width="720"(some fixed value as needed).
<StackPanel>
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
<!-- element x -->
</StackPanel>
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
<!-- element x -->
<!-- element x -->
</StackPanel>
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
<!-- element x -->
<!-- element x -->
<!-- element x -->
</StackPanel>
<StackPanel>
This produces the example you gave.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With