sofia.zaid_easylaser
Topic Author
Posts: 2
Joined: 13 Nov 2019, 15:59

TextBlock margins

13 Nov 2019, 16:14

Hello,
We have an element that is declared as following:
 <TextBlock x:Name="SubValue"     Grid.Row="2" Style="{StaticResource Style.TextBlock.Medium}" Background="Magenta" Margin="4,0,0,0" TextAlignment="Right" VerticalAlignment="Top" Text="-0.12"/>
Depending on the font size there is a margin on top of the text. We would like this margin to be smaller than it currently turns out to be. How can we achieve this?
See image below:

Image
 
User avatar
sfernandez
Site Admin
Posts: 2995
Joined: 22 Dec 2011, 19:20

Re: TextBlock margins

14 Nov 2019, 13:13

By margin do you mean the space between the number glyphs and the edge of the magenta rectangle?

Those bounds are determined by font line height, that you can adjust by using LineHeight and LineStackingStrategy properties:
<TextBlock Text="-0.12" TextAlignment="Right" VerticalAlignment="Top" Width="100"
    FontSize="30" Foreground="White" Background="Magenta"
    LineHeight="30" LineStackingStrategy="BlockLineHeight"/>
Another option that allows more control is to render the background using a Border and then adjust the TextBlock margins that can even be negative:
<Border Background="Magenta"  VerticalAlignment="Top" Width="100">
    <TextBlock Text="-0.12" TextAlignment="Right"
        FontSize="30" Foreground="White" Margin="0,-10,0,-7" />
</Border>
 
sofia.zaid_easylaser
Topic Author
Posts: 2
Joined: 13 Nov 2019, 15:59

Re: TextBlock margins

15 Nov 2019, 12:24

Hello,
Thanks, this solved it.
 
User avatar
sfernandez
Site Admin
Posts: 2995
Joined: 22 Dec 2011, 19:20

Re: TextBlock margins

15 Nov 2019, 20:39

Great, marking this post as solved.

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 21 guests