WPF 检测TextBlock是否超出内容显示范围

mac2025-08-31  9

/// <summary>         /// 判断文本框内容是否超出容器范围         /// </summary>         /// <param name="textBlock">需要判断的文本框控件</param>         /// <returns></returns>         public 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 > formattedText.MaxTextWidth;             return isTrimmed;         }

 

最新回复(0)