如何从 WPF TextBlock 中删除额外的填充?

发布于 2024-12-06 09:00:27 字数 352 浏览 7 评论 0原文

默认情况下,WPF TextBlock 似乎应用了额外的顶部和底部填充。我希望事实并非如此。

  • 我尝试设置负填充,但出现异常:

    <块引用>

    0,-10,0,0' 不是属性“Padding”的有效值。

  • 我尝试设置 LineHeight 属性,但没有明显效果.

这就是 TextBlock 在 Blend 中的外观。我用栗红色标记了有问题的部分。

红色区域应该被删除

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰之心 2024-12-13 09:00:27

一些研究和 HB 引导我找到了正确的答案,即设置以下属性:

<TextBlock LineStackingStrategy="BlockLineHeight"
           LineHeight="20"/> <!-- Or some other value you fancy. --> 

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. --> 
枫以 2024-12-13 09:00:27

这可能是默认情况下 Segoe UI 字体的一部分,例如尝试使用 Segoe(您不能分配负填充,但可以分配负边距,例如:Margin="0,-3,0,0"

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")

假装爱人 2024-12-13 09:00:27

该空间不是填充,而是字体的一部分,为字符上方和下方的重音符号保留。

接受的答案使行高小于字体高度。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文