Felix D.
Felix D.

Reputation: 5093

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.

Upvotes: 1

Views: 239

Answers (2)

Clemens
Clemens

Reputation: 128061

Set the TextAlignment property to Center:

<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"
           TextAlignment="Center"/>

Upvotes: 1

Vishnu Prasad V
Vishnu Prasad V

Reputation: 1248

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"/>

Upvotes: 1

Related Questions