Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elements starting by center of a stackpanel

Tags:

c#

.net

wpf

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?

like image 782
alansiqueira27 Avatar asked Aug 05 '11 20:08

alansiqueira27


People also ask

What is StackPanel?

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 you center something in WPF?

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

What is StackPanel C#?

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.


2 Answers

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

like image 117
Minustar Avatar answered Oct 19 '22 05:10

Minustar


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

like image 22
loxxy Avatar answered Oct 19 '22 03:10

loxxy