How to horizontally center an inline (Run) comparing to other inlines in WPF
Asked Answered
R

1

5

I have TextBlock in which I put 2 Inlines (Runs), the HorizontalAlignment of the TextBlock is set to Center, this is ok, I just want to center the first Run comparing to the second, here's my code :

<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="/Throne;component/Fonts/#Segoe UI Light" FontSize="35" FontWeight="Bold" Visibility="{Binding UserNameTextBlockVisibility}">
    <Run FontSize="25">En tant que :</Run>
    <LineBreak />
    <Run Text="{Binding UserName}" Foreground="ForestGreen"/>
</TextBlock>

Here's the result I get :

enter image description here

What I want to achieve :

enter image description here

I tried to search around the documentation and in threads on the internet, but I didn't really find a way to achieve this, how can I realize that ?

Riane answered 10/11, 2014 at 11:11 Comment(0)
M
13

Set TextAlignment="Center" on your TextBlock

 <TextBlock TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="/Throne;component/Fonts/#Segoe UI Light" FontSize="35" FontWeight="Bold" Visibility="{Binding UserNameTextBlockVisibility}">
    <Run FontSize="25">En tant que :</Run>
    <LineBreak />
    <Run Text="{Binding UserName}" Foreground="ForestGreen"/>
</TextBlock>
Myer answered 10/11, 2014 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.