您的位置:首页 > 其它

WPF TextBlock 判断 isTextTrimmed 文本是否超出

2017-08-08 10:19 836 查看
WPF TextBlock 设置TextTrimming情况下

判断 isTextTrimmed(Text 文本是否超出 是否出现了省略号)

    private bool HasTextTrimmed(TextBlock textBlock)
{
Typeface typeface = new Typeface(
textBlock.FontFamily,
textBlock.FontStyle,
textBlock.FontWeight,
textBlock.FontStretch);

FormattedText formattedText = new FormattedText(
textBlock.Text,
System.Threading.Thread.CurrentThread.CurrentCulture,
textBlock.FlowDirection,
typeface,
textBlock.FontSize,
textBlock.Foreground);

formattedText.MaxTextWidth = textBlock.ActualWidth;

bool isTrimmed = formattedText.Height > textBlock.ActualHeight ||
formattedText.Width >textBlock.MaxTextWidth;
       return isTrimmed;
     }


根据以上属性,可以做很多事,如文本超出时,设置ToolTip、文本替换等。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: