Orion31
Orion31

Reputation: 596

UWP - Center Text Vertically in TextBlock

So there is an existing thread which states that in order to vertically align text, you need to wrap in a border. However, I tried this, and instead of aligning vertically, the TextBlock went in a corner, still vertically aligned to the top. I don't know if this has something to do with the differences of WPF and UWP, but no matter, it didn't work. My TextBlock is defined as follows:

<TextBlock x:Name="TextBlock" Margin="855,226,0,0" TextWrapping="Wrap"
     VerticalAlignment="Top" Height="150" Width="150" FontFamily="DOCK11" 
     TextAlignment="Center" Padding="0" HorizontalAlignment="Left" Text="Sample Text..."/>`

With the given dimensions, would it be possible to vertically align my TextBlock's text? Thanks in advance.

Upvotes: 4

Views: 4849

Answers (1)

foxanna
foxanna

Reputation: 1570

According to the documentation TextBlock.TextAlignment

Gets or sets a value that indicates the horizontal alignment of text content.

To achieve your goal you indeed should wrap TextBlock with a Border and set it's VerticalAlignment to Center.

    <Border Height="150" 
            Width="150"
            Margin="855,226,0,0">
        <TextBlock TextWrapping="Wrap" 
                   FontFamily="DOCK11"
                   TextAlignment="Center" 
                   VerticalAlignment="Center"
                   Padding="0" 
                   Text="Sample Text..."/>
   </Border>

Upvotes: 4

Related Questions