Wrap TextBlock的最大行数

问题描述 投票:25回答:7

我有一个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 ...

任何解决方案

xaml windows-8 microsoft-metro textblock
7个回答
46
投票

Update (for UWP)

在UWP应用程序中,您不需要它,可以使用TextBlock属性MaxLines(请参阅MSDN


Original Answer:

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

Example:

TextBlock最多3行

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

这就是您需要的所有工作。

How to do this dynamically?

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

这只是你如何解决这个问题的基本解释!


3
投票

如果您设置了HeightTextWrappingTextTrimming,它的行为将完全符合您的要求:

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

上面的代码将包含两行,然后使用CharacterEllipsis超过该点。


2
投票

你需要在你的TextTrimming="WordEllipsis"设置TextBlock


2
投票

根据tobi.at和gt的回答,我创建了这个MaxLines行为。至关重要的是,它不依赖于通过计算字体的行高来设置LineHeight属性。你仍然需要为它设置TextWrappingTextTrimming,你可以根据需要渲染TextBox

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

还有一个MinLines行为可以是不同的或设置为与MaxLines行为相同的数字来设置行数。

public class NumLinesBehaviour : Behavior<TextBlock>
{
    TextBlock textBlock => AssociatedObject;

    protected override void OnAttached()
    {
        base.OnAttached();
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
    }

    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;
    }
}

0
投票

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

public class LineHeightBehavior
{
    public static readonly DependencyProperty MaxLinesProperty =
        DependencyProperty.RegisterAttached(
            "MaxLines",
            typeof(int),
            typeof(LineHeightBehavior),
            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)
    {
        var element = d as TextBlock;
        if (element != null)
        {
            element.MaxHeight = element.LineHeight * GetMaxLines(element);
        }
    }
}

默认情况下,LineHeight设置为double.NaN,因此必须首先手动设置此值。

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

<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>

0
投票

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


-4
投票

我怀疑这是可配置的,Wrapping是基于许多因素,如font-size / kerning,textblock的可用宽度(horizo​​ntalalignment = stretch可以产生很大的不同),parent的面板类型(scrollviewer / stackpanel / grid)等。

如果您希望文本明确地流向下一行,则应使用“运行”块,然后对该运行块使用类型省略的包装。

© www.soinside.com 2019 - 2024. All rights reserved.