WPF TextBlock IsTextTrimmed 判断文本是否超出

简介: 原文:WPF TextBlock IsTextTrimmed 判断文本是否超出WPF TextBlock 设置TextTrimming情况下 判断 isTextTrimmed(Text 文本是否超出 是否出现了省略号)     private bool IsTextTrimmed(Te...
原文: WPF TextBlock IsTextTrimmed 判断文本是否超出

WPF TextBlock 设置TextTrimming情况下

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

    private bool IsTextTrimmed(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;
     }

 FormattedText:绘制文本,也可以根据当前控件样式(最大高宽/字体样式),获取当前控件的最大容纳字符数。

根据以上属性,可以做很多事,如文本超出时,设置ToolTip、文本替换等。

目录
相关文章
|
9月前
|
C# Windows
WPF技术之TextBlock控件
WPF(Windows Presentation Foundation)的TextBlock控件是用于显示文本的控件。与Label控件相比,TextBlock提供了更多的灵活性和格式化选项。
328 1
WPF中给TextBox/TextBlock设置提示文本
WPF中给TextBox/TextBlock设置提示文本
WPF中给TextBox/TextBlock设置提示文本
|
C#
WPF自定义行为Behavior,实现双击控件复制文本
原文:WPF自定义行为Behavior,实现双击控件复制文本 WPF引用xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.
1039 0
|
C# 前端开发 容器
WPF中如何在文本外面加虚线外框
原文:WPF中如何在文本外面加虚线外框     昨天突然被问到如何在wpf里面给一段文本加个虚线外框,由于有一段时间没玩wpf了,一时还真没想出来,虽然大概有个思路,但是也不保证正确。今天回到家,闲着没事情也就随便试验了一下。
1032 0
|
C#
【C#/WPF】TextBlock/TextBox/Label编辑文字的问题
原文:【C#/WPF】TextBlock/TextBox/Label编辑文字的问题 标题有点描述不清,就当是为了方便自己用时易于搜索到。
1345 0
|
Linux C# Windows
WPF中TextBlock文本换行与行间距
原文:WPF中TextBlock文本换行与行间距 换行符: C#代码中:\r\n 或  \r 或 \n      XAML中:
 或 
 注:\r 回车 (carriage return 缩写),\n 新行 (new line 缩写)。
2471 0
|
C#
【msdn wpf forum翻译】TextBlock等类型的默认样式(implicit style)为何有时不起作用?
原文:【msdn wpf forum翻译】TextBlock等类型的默认样式(implicit style)为何有时不起作用?原文链接:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/148e95c6-6fb5-4399-8a56-4...
945 0
|
C#
【msdn wpf forum翻译】TextBox中文本 中对齐 的方法
原文:【msdn wpf forum翻译】TextBox中文本 中对齐 的方法原文链接:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/49864e35-1dbf-4292-a361-93f1a8400558 问题:TextBox中文本中对齐,使用 TextBox.HorizontalContentAlignment="Center"行不通(TextBox.VerticalContentAlignment="Center"则会起到预期的作用。
1197 0
|
C#
WPF控件TextBlock中文字自动换行
原文:WPF控件TextBlock中文字自动换行 在很多的WPF项目中,往往为了追求界面的美观,需要控制控件中文字的换行显示,现对TextBlock控件换行的实现方式进行总结,希望大家多多拍砖!!! 1.
2770 0
|
22天前
|
C# 开发者 Windows
基于Material Design风格开源、易用、强大的WPF UI控件库
基于Material Design风格开源、易用、强大的WPF UI控件库