How to remove additional padding from a WPF TextBlock?

ぐ巨炮叔叔 提交于 2019-12-17 15:54:25

问题


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.


回答1:


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. --> 



回答2:


This is probably part of the font which is Segoe UI by default, try Segoe instead for example. (You cannot assign negative padding but you could assign negative Margins, e.g.: Margin="0,-3,0,0")




回答3:


This space is not padding, but part of the font, reserved for accents above and below characters.

The accepted answer makes the line height smaller than the font height.



来源:https://stackoverflow.com/questions/7543846/how-to-remove-additional-padding-from-a-wpf-textblock

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!