WPF 文本块截止

发布于 2024-10-30 08:22:16 字数 115 浏览 1 评论 0原文

你好,Guyz,我有一个固定宽度的 WPF TextBlock,比如 100 ,如果字符串不适合宽度,则最后一个字符总是被截断,因为所有字符的大小不同。我不想剪切字符,而是想从那里跳过文本,只显示没有字符剪切的文本。

Hi Guyz I have a WPF TextBlock of fixed width say 100 , If the string doesnt fit in the width the last character is being cutoff always as all the characters are of not the same size. I dont want to cut the character instead I want to skip the text from there and just display the text with no character cutoff.

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

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

发布评论

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

评论(2

寄离 2024-11-06 08:22:16

您有几个选项来控制文本的换行和剪切:

  • TextWrapping 可用于使文本流到下一行
  • TextTrimming 可用于决定如何剪切不适合的文本

TextTrimming=None (默认)意味着不适合的文本将被隐藏,但它可能会削减字符的中间,这听起来像是您的问题描述。

TextTrimming=WordEllipsisTextTrimming=CharacterEllipsis 将避免显示半个字符,但会在文本末尾附加“...”。这对用户来说可能看起来更好。

如果您想剪掉多余的字符而不添加省略号,则必须使用该技术 Ed S. 描述

You have a couple of options to control wrapping and cutting of text:

  • TextWrapping can be used to make the text flow to the next line
  • TextTrimming can be used to decide how to cut text that doesn't fit

TextTrimming=None (the default) will mean that text which doesn't fit will be hidden, but it may cut down the middle of a character, which sounds like the problem you describe.

TextTrimming=WordEllipsis or TextTrimming=CharacterEllipsis will avoid showing half a character, but will append "..." to the end of the text. That will probably look better to users.

If you want to cut off the extra characters without adding the ellipsis, you'd have to use the technique Ed S. described

心作怪 2024-11-06 08:22:16

我想我不太明白你在这里的用例。我的第一个建议是简单地动态调整 TextBlock 的大小。如果这是不可能的,那么您将必须获取字符串的宽度并自行操作它,然后再将其设置在 TextBlock 中(或者使用固定宽度字体,假设您可以并且知道字符串的最大长度)。

如果您需要在显示之前测量字符串的宽度,可以使用 FormattedText 类 来执行此操作。

I suppose that I don't really understand your use case here. My first suggestion would be to simply dynamically size your TextBlock. If that's not possible then you wil have to get the width of the string and manipulate it yourself before you set it in the TextBlock (or use a fixed width font assuming that you can and you know the max length of the string).

If you need to measure the width of the string before it is displayed you can use the FormattedText class to do so.

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