Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove additional padding from a WPF TextBlock?

By default a WPF TextBlock seems to have additional top and bottom padding applied. I wish this wasn't so.

  • I've tried setting negative padding, but got an exception:

    0,-10,0,0' is not a valid value for property 'Padding'.

  • I've tried setting the LineHeight property, to no apparent effect.

This is how the TextBlock looks in Blend. I've marked the problematic portion with maroon red.

the red area should be removed

like image 564
Gleno Avatar asked Sep 25 '11 05:09

Gleno


People also ask

Is TextBlock editable?

TextBlock is not editable.

What is padding in WPF?

Padding represents the distance between the side of the control (which can be the margin) and its content. The content depends on the type of the control. Margin is outside the UI element, while Padding is inside it. Next Recommended Reading WPF: Textblock Vs Label.

How do I center TextBlock in WPF?

If you want to center each line, use a TextBlock instead, and set TextAlignment="Center" .

What is WPF TextBlock?

The TextBlock control provides flexible text support for UI scenarios that do not require more than one paragraph of text. It supports a number of properties that enable precise control of presentation, such as FontFamily, FontSize, FontWeight, TextEffects, and TextWrapping.


1 Answers

Some research and H.B. guided me to the right answer, which is setting the following properties:

<TextBlock LineStackingStrategy="BlockLineHeight"            LineHeight="20"/> <!-- Or some other value you fancy. -->  
like image 62
Gleno Avatar answered Oct 02 '22 13:10

Gleno