views:

3056

answers:

5

How to make vertical center alignment to the text inside a TextBlock? I found TextAlignment property. But it is for horizontal text alignment. How to do it for vertical text alignment.

+2  A: 

The TextBlock doesn't support vertical text alignment.

I work around this by wrapping the text block with a Grid and setting HorizontalAlignment="Stretch" and VerticalAlignment="Center".

Like this:

    <Grid>
        <TextBlock 
            HorizontalAlignment="Stretch"
            VerticalAlignment="Center"
            Text="Your text" />
    </Grid>
hwiechers
A: 

I'm a bit confused.

Does TextBlock.VerticalAlignment (technically it's FrameworkElement.VerticalAlignment) solve your problem?

Or are you trying to accomplish something else?

La Cabra
no, its the text inside the TextBlock (or FrameworkElement) not the Element itself
Aaron Hoffman
+1  A: 

I guess there is no out-of-the-box solution.
I reported this issue to Microsoft, please vote: https://connect.microsoft.com/WPF/feedback/ViewFeedback.aspx?FeedbackID=523432

Shimmy
+3  A: 

A Textblock itself can't do vertical alignment

The best way to do this that I've found is to put the textblock inside a border, so the border does the alignment for you.

<Border BorderBrush="{x:Null}" Height="50">
    <TextBlock TextWrapping="Wrap" Text="Some Text" VerticalAlignment="Center"/>
</Border>

Note: This is functionally equivalent to using a grid, it just depends how you want the controls to fit in with the rest of your layout as to which one is more suitable

Orion Edwards
A: 

For me, VerticalAlignment="Center" fixes this problem.
This could be because the TextBlock is wrapped in a grid, but then so is practically everything in wpf.