Abhishek Gahlout
Abhishek Gahlout

Reputation: 3462

how to put a control vertically in center in stack panel?

How can I put a control vertically in the center in a stackpanel? I am doing as below:

<StackPanel Name="stackPanel1" Height="100" Width="200"
            HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Button Content="Hello" Height="25" Width="100"
            HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>

The UI is rendering as below image. I want the button control to be placed in between of the stackpanel vertically. How can I achieve this without using Margin.

enter image description here

Upvotes: 0

Views: 239

Answers (1)

Martin Liversage
Martin Liversage

Reputation: 106826

I am not sure I fully understand what you want to achieve but you can center the StackPanel inside a Grid. Whatever you put inside the StackPanel will be stacked as expected.

<Grid Height="100" Width="200">
  <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
    <Button Content="Hello" Height="25" Width="100"/>
    <Button Content="Hello" Height="25" Width="100"/>
 </StackPanel>
</Grid>

This is what it looks like in the XAML designer. The blue outline is the StackPanel and the gray outline is the Grid.

StackPanel inside Grid

Upvotes: 1

Related Questions