Wrap TextBlock 的最大行数 [英] Maximum number of lines for a Wrap TextBlock
问题描述
我有一个具有以下设置的 TextBlock
:
I have a TextBlock
with the following setting:
TextWrapping="Wrap"
我可以确定最大行数吗?
Can I determine the maximum number of lines?
例如考虑以下字符串TextBlock.Text
:
This is a very good horse under the blackboard!!
目前已显示如下:
This is a very
good horse under
the blackboard!!
我需要变成这样:
This is a very
good horse ...
有什么解决办法吗?
推荐答案
更新(适用于 UWP)
在 UWP 应用中,您不需要它,可以使用 TextBlock 属性 MaxLines
(请参阅 MSDN)
如果您有特定的 LineHeight
,您可以计算 TextBlock 的最大高度.
If you have a specific LineHeight
you can calculate the maximum height for the TextBlock.
TextBlock 最多 3 行
<TextBlock
Width="300"
TextWrapping="Wrap"
TextTrimming="WordEllipsis"
FontSize="24"
LineStackingStrategy="BlockLineHeight"
LineHeight="28"
MaxHeight="84">YOUR TEXT</TextBlock>
这是让您的要求工作所需的全部内容.
This is all that you need to get your requirement working.
只需在 C#/VB.NET 中创建一个扩展 TextBlock
的新控件并给它一个新的 DependencyProperty
int MaxLines.
然后覆盖OnApplyTemplate()
方法并根据LineHeight
* MaxLines
设置MaxHeight
.
Just create a new control in C#/VB.NET that extends TextBlock
and give it a new DependencyProperty
int MaxLines.
Then override the OnApplyTemplate()
method and set the MaxHeight
based on the LineHeight
* MaxLines
.
这只是关于如何解决这个问题的基本解释!
That's just a basic explanation on how you could solve this problem!
这篇关于Wrap TextBlock 的最大行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!