Search code examples
c#wpfxamlalignmentword-wrap

Textalign in WPF


I have a short question about Alignments.

I am using a TextBlock in XAML.

When having a small window (Widht > 200) and a long Text I have to enable TextWrapping.

No problem this far.

The problem is that when I want to align the Text in Center the wrapped part is aligned Left again.

Is there a way to get rid of it or do I need to wrap it myself in code/use two TextBlocks ?

Code:

<TextBlock Margin="5"                
           VerticalAlignment="Center"
           HorizontalAlignment="Center"
           Text="This is a Test. This is a Test. This is a Test. This is a Test."
           TextWrapping="Wrap"/>

How it looks like:

enter image description here

What I need:

Something like:

This is a Test. This is a Test. This is a Test.
                This is a Test.

Solution

  • You should use TextAlignment attribute for aligning the text Center.

    Adding this Attribute to the TextBlock will make it work

    <TextBlock Margin="5"                
               VerticalAlignment="Center"
               HorizontalAlignment="Center"
               Text="This is a Test. This is a Test. This is a Test. This is a Test."
               TextAlignment="Center"
               TextWrapping="Wrap"/>