Geoff James
Geoff James

Reputation: 3180

XAML TextBlock TextAlignment vs HorizontalAlignment

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?

Upvotes: 5

Views: 4770

Answers (1)

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

TextAlignment determines the alignment of the text within the TextBox

Upvotes: 5

Related Questions