Emircan Aydın
Emircan Aydın

Reputation: 61

HorizontalAlignment problem in stackpanel

HorizontalAlignment setting of textblock does not work when I add 2 textblock in stackpanel

what is the reason of this ?

Xaml:

<Border Grid.Column="1" Background="#312a28">
    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
        <TextBlock Name="current_page" Text="ÖZET" VerticalAlignment="Center" Foreground="White" FontSize="16"/>
        <TextBlock Name="next_page" Text="KASA" VerticalAlignment="Center" HorizontalAlignment="Right" Foreground="White" FontSize="16"/>
   </StackPanel>
</Border>

Upvotes: 1

Views: 982

Answers (2)

kaqq
kaqq

Reputation: 355

You can't refer to the right border of "StackPanel Orientation="Horizontal". StackPanel is filled with elements one by one according to their size. Use grid instead or set margin of the element.

<Border Grid.Column="1" Background="#312a28">
    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
        <TextBlock Name="current_page" Text="ÖZET" VerticalAlignment="Center" Foreground="White" FontSize="16"/>
        <TextBlock Name="next_page" Text="KASA" VerticalAlignment="Center" Margin="5,0,0,0" Foreground="White" FontSize="16"/>
   </StackPanel>
</Border>

Upvotes: 0

ASh
ASh

Reputation: 35680

StackPanel with Orientation="Horizontal" ignores HorizontalAlignment of children elements.

if you need layout like

=======================
| (ÖZET)       (KASA) |
=======================

then try Grid:

<Border Grid.Column="1" Background="#312a28">
   <Grid VerticalAlignment="Center">
      <TextBlock Name="current_page" Text="ÖZET" VerticalAlignment="Center" Foreground="White" FontSize="16"/>
      <TextBlock Name="next_page" Text="KASA" VerticalAlignment="Center" HorizontalAlignment="Right" Foreground="White" FontSize="16"/>
   </Grid>
</Border>

Upvotes: 7

Related Questions