gpt4 book ai didi

xaml - Wrap TextBlock的最大行数

转载 作者:行者123 更新时间:2023-12-03 10:35:28 24 4
gpt4 key购买 nike

我有一个带有以下设置的TextBlock:

TextWrapping="Wrap"

我可以确定最大行数吗?

例如,考虑以下字符串 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 Apps中,您不需要它,可以使用TextBlock属性MaxLines(请参阅MSDN)

原始答案:

如果您有特定的LineHeight,则可以计算TextBlock的最大高度

例:

最多3行的TextBlock

<TextBlock 
Width="300"
TextWrapping="Wrap"
TextTrimming="WordEllipsis"
FontSize="24"
LineStackingStrategy="BlockLineHeight"
LineHeight="28"
MaxHeight="84">YOUR TEXT</TextBlock>

这就是使需求正常工作所需的全部。

如何动态地做到这一点?

只需在C#/ VB.NET中创建一个扩展 TextBlock的新控件,并为其提供一个新的 DependencyProperty int MaxLines。
然后重写 OnApplyTemplate()方法,并根据 MaxHeight * LineHeight设置 MaxLines

这只是有关如何解决此问题的基本说明!

关于xaml - Wrap TextBlock的最大行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13637814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com