Xaml 换行文本块的最大行数

Xaml 换行文本块的最大行数,xaml,uwp,windows-8,uwp-xaml,textblock,Xaml,Uwp,Windows 8,Uwp Xaml,Textblock,我有一个具有以下设置的TextBlock: TextWrapping="Wrap" 我可以确定最大行数吗 例如,考虑以下字符串文本块.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 ... 有什么解决方案吗?您需要在TextBl

我有一个具有以下设置的
TextBlock

TextWrapping="Wrap"
我可以确定最大行数吗

例如,考虑以下字符串<代码>文本块.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 ...

有什么解决方案吗?

您需要在
TextBlock
中设置
texttiming=“WordEllipsis”
我怀疑这是可配置的,包装是基于许多因素的,例如字体大小/字距、文本块的可用宽度(horizontalalignment=stretch可能会产生很大的差异)、父面板类型(scrollviewer/stackpanel/grid)等

如果希望文本显式地流向下一行,则应使用“Run”块,然后对该Run块使用省略号包装。

Update(对于UWP) 在UWP应用程序中,您不需要此功能,可以使用TextBlock属性
MaxLines
(请参阅)


原始答复: 如果您有特定的
行高
,则可以计算文本块的最大高度

例子: 文本块,最多3行

您的文本
这就是让您的需求发挥作用所需的全部

如何动态地执行此操作? 只需在C#/VB.NET中创建一个扩展
TextBlock
的新控件,并给它一个新的
dependencProperty
int MaxLines。
然后重写应用程序模板()的
OnApplyTemplate()
方法,并基于
LineHeight
*
MaxLines
设置
MaxHeight


这只是关于如何解决此问题的基本解释!

如果您设置了
高度
文本包装
,以及
文本修剪
所有设置,它的行为将完全符合您的要求:

<TextBlock Height="60" FontSize="22" FontWeight="Thin"
    TextWrapping="Wrap" TextTrimming="CharacterEllipsis">


上面的代码将换行为两行,然后使用
字符省略
超出该点。

基于@artistandsocial的回答,我创建了一个附加属性以编程方式设置最大行数(而不是必须重载
文本块
,这在WPF中是不受鼓励的)

默认情况下,
LineHeight
设置为
double.NaN
,因此必须首先手动设置此值,否则将从
TextBlock
FontFamily
FontSize
计算高度

然后可以在
样式中设置附加的属性
MaxLines
和其他相关属性:

<Style TargetType="{x:Type TextBlock}"
       BasedOn="{StaticResource {x:Type TextBlock}}">
    <Setter Property="TextTrimming"
            Value="CharacterEllipsis" />
    <Setter Property="TextWrapping"
            Value="Wrap" />
    <Setter Property="LineHeight"
            Value="16" />
    <Setter Property="LineStackingStrategy"
            Value="BlockLineHeight" />
    <Setter Property="behaviors:LineHeightBehavior.MaxLines"
            Value="2" />
</Style>


对于任何开发UWP或WinRT应用程序的人来说,
TextBlock
都有一个可以设置的属性。

基于tobi.at和gt的回答,我创建了这个
MaxLines
行为。重要的是,它不依赖于通过从字体计算行高来设置
LineHeight
属性。您仍然需要设置
TextWrapping
texttiming
对于它,
TextBox
将按照您的意愿呈现

<TextBlock behaviours:NumLinesBehaviour.MaxLines="3" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" Text="Some text here"/>

谢谢,但只有当文本到达
TextBlock
的末尾时,这才有效,可能是2行或更多。我希望
TextBlock
在后面修剪文本,例如正好是2行。@MBZ为此,您需要编写自定义转换器,通过
TextBlock
文本并在需要的地方插入省略号。如果您可以设置hei文本块、修剪和包装集的ght应该是您所需要的所有内容。请参阅下面@kindasimple的评论,这将为您提供所需内容。为什么选择此作为答案?在删除不必要的内容后,如“:Behavior”下面几行代码显然是从行为基类派生的,它工作起来很有魅力,所以只使用一个具有dependencyProperties的静态类。
<Style TargetType="{x:Type TextBlock}"
       BasedOn="{StaticResource {x:Type TextBlock}}">
    <Setter Property="TextTrimming"
            Value="CharacterEllipsis" />
    <Setter Property="TextWrapping"
            Value="Wrap" />
    <Setter Property="LineHeight"
            Value="16" />
    <Setter Property="LineStackingStrategy"
            Value="BlockLineHeight" />
    <Setter Property="behaviors:LineHeightBehavior.MaxLines"
            Value="2" />
</Style>
<TextBlock behaviours:NumLinesBehaviour.MaxLines="3" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" Text="Some text here"/>
public class NumLinesBehaviour : Behavior<TextBlock>
{
    TextBlock textBlock => AssociatedObject;

    public static readonly DependencyProperty MaxLinesProperty =
        DependencyProperty.RegisterAttached(
            "MaxLines",
            typeof(int),
            typeof(NumLinesBehaviour),
            new PropertyMetadata(default(int), OnMaxLinesPropertyChangedCallback));

    public static void SetMaxLines(DependencyObject element, int value)
    {
        element.SetValue(MaxLinesProperty, value);
    }

    public static int GetMaxLines(DependencyObject element)
    {
        return (int)element.GetValue(MaxLinesProperty);
    }

    private static void OnMaxLinesPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TextBlock element = d as TextBlock;
        element.MaxHeight = getLineHeight(element) * GetMaxLines(element);
    }

    public static readonly DependencyProperty MinLinesProperty =
        DependencyProperty.RegisterAttached(
            "MinLines",
            typeof(int),
            typeof(NumLinesBehaviour),
            new PropertyMetadata(default(int), OnMinLinesPropertyChangedCallback));

    public static void SetMinLines(DependencyObject element, int value)
    {
        element.SetValue(MinLinesProperty, value);
    }

    public static int GetMinLines(DependencyObject element)
    {
        return (int)element.GetValue(MinLinesProperty);
    }

    private static void OnMinLinesPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TextBlock element = d as TextBlock;
        element.MinHeight = getLineHeight(element) * GetMinLines(element);
    }

    private static double getLineHeight(TextBlock textBlock)
    {
        double lineHeight = textBlock.LineHeight;
        if (double.IsNaN(lineHeight))
            lineHeight = Math.Ceiling(textBlock.FontSize * textBlock.FontFamily.LineSpacing);
        return lineHeight;
    }
}