5

Consider the following XAML:

<Grid>
    <TextBlock Text="Some Text" TextAlignment="Right" />
    <TextBlock Text="Some Text" HorizontalAlignment="Right" />
</Grid>

So long as both TextBlocks are set to the same Grid.Row and Grid.Column, they will always appear in the same place - one on top of the other.

Similar concept, here - with the 2 lines of text in both the StackPanels aligning exactly:

<Grid>
    <StackPanel>
        <TextBlock Text="Line One" HorizontalAlignment="Right" />
        <TextBlock Text="Line Two" TextAlignment="Right" />
    </StackPanel>
    <StackPanel>
        <TextBlock Text="Line One" HorizontalAlignment="Right" />
        <TextBlock Text="Line Two" TextAlignment="Right" />
    </StackPanel>
</Grid>

And so on...


In terms of displaying the text to the right of the parent, the TextAlignment and HorizontalAlignment attributes are both doing the same thing - from what I can see.

Can anybody tell me what the actual difference between TextAlignment and HorizontalAlignment is, when it comes to TextBlocks?

Is there a preferred choice of the two to use?

Are there any implications to using either?

Geoff James
  • 3,122
  • 1
  • 17
  • 36

1 Answers1

5

HorizontalAlignment determines the alignment of the TextBox relative to its parent.

TextAlignment determines the alignment of the text within the TextBox

  • 1
    Well dang, beat me while making an example, if you want another image for visual I was going to slap [this image](http://i.imgur.com/0risUbE.png) on mine to visualize like a "center" – Chris W. Dec 02 '16 at 15:54
  • Thank you both @EdPlunkett and ChrisW.! That's a superb explanation and some great example illustrations :) This had made it a lot clearer to see the differences. Cheers, guys! – Geoff James Dec 02 '16 at 16:08