I just received a requirement to display a length of text in a control. The control is of a particular width and will be up to 2 lines in height. If it renders longer than two lines it will just display "..." at the end of the string.
我刚收到要求在控件中显示一段文本的要求。控件具有特定的宽度,高度最多为2行。如果它渲染超过两行,它将只在字符串的末尾显示“...”。
Is this possible with any of the stock standard WPF controls?
这是否可以使用任何库存标准WPF控件?
Thanks, D.
2 个解决方案
#1
15
Set the Height of the TextBlock to be high enough to fit two lines. Set the TextWrapping to Wrap and the TextTrimming to CharacterEllipsis or WordEllipsis.
将TextBlock的高度设置为足够高以适合两行。将TextWrapping设置为Wrap,将TextTrpping设置为CharacterEllipsis或WordEllipsis。
For the default Segoe UI 12Pt font, I find this does it
对于默认的Segoe UI 12Pt字体,我发现这样做
<TextBlock TextWrapping="Wrap" Height="40" TextTrimming="CharacterEllipsis" />
You could probably do some code behind to work out the height it should be be for a particular font if you want.
如果你愿意的话,你可以在后面编写一些代码来计算特定字体的高度。
#2
0
That's not standard behavior that I've ever found, but again I've not looked for it.
这不是我所发现的标准行为,但我再也没有找到它。
One possibility is to use a monospace font in a TextArea control, and then if the string is greater than however many characters fit in the area, only display the right N characters with the ellipses
一种可能性是在TextArea控件中使用等宽字体,然后如果字符串大于该区域中适合的多个字符,则只显示带有省略号的正确N个字符
#1
15
Set the Height of the TextBlock to be high enough to fit two lines. Set the TextWrapping to Wrap and the TextTrimming to CharacterEllipsis or WordEllipsis.
将TextBlock的高度设置为足够高以适合两行。将TextWrapping设置为Wrap,将TextTrpping设置为CharacterEllipsis或WordEllipsis。
For the default Segoe UI 12Pt font, I find this does it
对于默认的Segoe UI 12Pt字体,我发现这样做
<TextBlock TextWrapping="Wrap" Height="40" TextTrimming="CharacterEllipsis" />
You could probably do some code behind to work out the height it should be be for a particular font if you want.
如果你愿意的话,你可以在后面编写一些代码来计算特定字体的高度。
#2
0
That's not standard behavior that I've ever found, but again I've not looked for it.
这不是我所发现的标准行为,但我再也没有找到它。
One possibility is to use a monospace font in a TextArea control, and then if the string is greater than however many characters fit in the area, only display the right N characters with the ellipses
一种可能性是在TextArea控件中使用等宽字体,然后如果字符串大于该区域中适合的多个字符,则只显示带有省略号的正确N个字符