WPF 文本块截止
你好,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有几个选项来控制文本的换行和剪切:
TextWrapping
可用于使文本流到下一行TextTrimming
可用于决定如何剪切不适合的文本TextTrimming=None
(默认)意味着不适合的文本将被隐藏,但它可能会削减字符的中间,这听起来像是您的问题描述。TextTrimming=WordEllipsis
或TextTrimming=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 lineTextTrimming
can be used to decide how to cut text that doesn't fitTextTrimming=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
orTextTrimming=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
我想我不太明白你在这里的用例。我的第一个建议是简单地动态调整 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.