MBZ*_*MBZ 25 xaml textblock microsoft-metro windows-8
我有TextBlock
以下设置:
TextWrapping="Wrap"
Run Code Online (Sandbox Code Playgroud)
我可以确定最大行数吗?
例如,考虑以下字符串TextBlock.Text
:
This is a very good horse under the blackboard!!
Run Code Online (Sandbox Code Playgroud)
它目前已经显示如下:
This is a very
good horse under
the blackboard!!
Run Code Online (Sandbox Code Playgroud)
我需要这样做:
This is a very
good horse ...
Run Code Online (Sandbox Code Playgroud)
任何解决方案
tob*_*.at 46
在UWP Apps中,您不需要它,可以使用TextBlock属性MaxLines
(请参阅MSDN)
如果您有特定的LineHeight
,则可以计算TextBlock 的最大高度.
TextBlock最多3行
<TextBlock
Width="300"
TextWrapping="Wrap"
TextTrimming="WordEllipsis"
FontSize="24"
LineStackingStrategy="BlockLineHeight"
LineHeight="28"
MaxHeight="84">YOUR TEXT</TextBlock>
Run Code Online (Sandbox Code Playgroud)
这就是您需要的所有工作.
只需在C#/ VB.NET中创建一个新的控件,它扩展TextBlock
并为其提供一个新的DependencyProperty
int MaxLines.
然后覆盖该OnApplyTemplate()
方法并MaxHeight
基于LineHeight
*设置MaxLines
.
这只是你如何解决这个问题的基本解释!
根据 tobi.at 和 gt 的回答,我创建了这种MaxLines
行为。至关重要的是,它不依赖于LineHeight
通过计算字体的行高来设置属性。您仍然需要设置TextWrapping
并TextTrimming
使其TextBox
按照您的意愿进行渲染。
<TextBlock behaviours:NumLinesBehaviour.MaxLines="3" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" Text="Some text here"/>
Run Code Online (Sandbox Code Playgroud)
还有一种MinLines
行为可以与设置行数的行为不同或设置为相同的数字MaxLines
。
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;
}
}
Run Code Online (Sandbox Code Playgroud)
如果你有Height
,TextWrapping
和TextTrimming
所有的设置,它的行为完全像你想要的:
<TextBlock Height="60" FontSize="22" FontWeight="Thin"
TextWrapping="Wrap" TextTrimming="CharacterEllipsis">
Run Code Online (Sandbox Code Playgroud)
上面的代码将最多包含两行,然后CharacterEllipsis
在该点之后使用。