文章出處

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、文本替換等。


文章列表


不含病毒。www.avast.com
全站熱搜
創作者介紹
創作者 大師兄 的頭像
大師兄

IT工程師數位筆記本

大師兄 發表在 痞客邦 留言(0) 人氣()