[WPF] TextBlock的MaxHeight,长度表达式如何写,让其最多显示两行

lgg06 2008-07-08 10:14:45
现在想让一TextBlock 最多显示两行(在Xaml中设置), 不知其MaxHeight的表达式应该如果写, 也不知道哪里有相关的资料可参考

大侠帮帮忙,谢谢!
...全文
653 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
weng3998 2012-10-07
  • 打赏
  • 举报
回复
没有那么复杂 最高 是字的两倍多点 就行了
<TextBlock Margin="2,5" TextWrapping="Wrap" Text="Oracle server name:" HorizontalAlignment="Right" VerticalAlignment="Center" FontSize="13.333" MaxHeight="38" />
lgg06 2008-07-12
  • 打赏
  • 举报
回复
万分感谢!

打工的没办法,PD怎么说就得怎样, QA也只是对着SPEC报Bug, 呵呵
zhouyongh 2008-07-11
  • 打赏
  • 举报
回复
说实话,这么搞没啥意思。如果要限制行数,一行就够了,或者加个文字滚动,有时候也应该劝劝客户...

给你写了个TextBlockAttacher,可以只在XAML中设置。

public class TextBlockAttacher
{
private const double FontSizeConstant = (double)4 / 3; // 1/3 = lines spacing

public static readonly DependencyProperty LimitedLineNumberProperty = DependencyProperty.RegisterAttached
("LimitedLineNumber",
typeof(int),
typeof(TextBlockAttacher),
new FrameworkPropertyMetadata(new PropertyChangedCallback(LimitedLineNumberChanged)));

public static void SetLimitedLineNumber(DependencyObject target, int value)
{
target.SetValue(TextBlockAttacher.LimitedLineNumberProperty, value);
}

private static void LimitedLineNumberChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
TextBlock txtBlock = target as TextBlock;

if (txtBlock != null)
{
if (((int)e.NewValue != 0) && ((int)e.OldValue == 0))
{
txtBlock.Loaded += txtBlock_Loaded;
}
else if (((int)e.NewValue == 0) && ((int)e.OldValue != 0))
{
txtBlock.Loaded -= txtBlock_Loaded;
}
}
}

private static void txtBlock_Loaded(object sender, RoutedEventArgs e)
{
TextBlock txtBlock = sender as TextBlock;
if (txtBlock != null)
{
int lineNumber = (int)txtBlock.GetValue(TextBlockAttacher.LimitedLineNumberProperty);
double fontSize = txtBlock.FontSize * FontSizeConstant;
txtBlock.MaxHeight = lineNumber * fontSize;
}
}

}


use like this:
[code=XAML]
<TextBlock src:TextBlockAttacher.LimitedLineNumber="2" FontSize="50" TextWrapping="Wrap" Text="Yohan Zhou 12122dasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasda" >
</TextBlock>
[/code]

Hope this helps
lgg06 2008-07-10
  • 打赏
  • 举报
回复
自己再顶
lgg06 2008-07-08
  • 打赏
  • 举报
回复
不好意思, 是TextBlock, 这个好像没有MaxLines 属性

Thanks all the same!
zhouyongh 2008-07-08
  • 打赏
  • 举报
回复
[code=XAML]
<TextBox TextWrapping="Wrap" MaxLines="2" AcceptsReturn="True"/>
[/code]

Hope helps
lgg06 2008-07-08
  • 打赏
  • 举报
回复
自己顶

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧