MSDN > 論壇首頁 > Windows Presentation Foundation (WPF) > How to judge text block is trimming with ...
發問發問
 

已答覆How to judge text block is trimming with ...

  • 2008年8月28日 上午 08:29anders06 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     包含代碼
    I have set text block TextTrimming property to WordEllipsis. And i only want to show tooltip when the text block does not show the whole text.
    Currently, i used the following mehtod,  but the precision is not too good.

    FormattedText fText = new FormattedText(txtBlock.Text, System.Globalization.CultureInfo.CurrentUICulture, FlowDirection.LeftToRight,  
                        new Typeface(txtBlock.FontFamily, txtBlock.FontStyle, txtBlock.FontWeight, txtBlock.FontStretch), txtBlock.FontSize, Brushes.Black);  
    Rect desiredRect = fText.BuildGeometry(new Point(0, 0)).Bounds;  
    double desiredWidth = desiredRect.Width + (txtBlock.ActualHeight - desiredRect.Height) - 1;  
     
    if (desiredWidth > txtBlock.ActualWidth)  
    {  
         //...  
     


    anders06

解答

  • 2008年9月2日 上午 06:18Marco Zhou 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    I think you could use the following extension method to determine if a specified TextBlock has been trimmed without calling BuildGeometry() to get the size of the underlying geomtrical metrics which could be different from my personal experience:

    public static class TextBlockExtensions
    {
        public static Boolean IsTextTrimmed(this TextBlock textBlock)
        {
            Typeface typeface = new Typeface(textBlock.FontFamily,
                textBlock.FontStyle,
                textBlock.FontWeight,
                textBlock.FontStretch);

            // FormattedText is used to measure the whole width of the text held up by TextBlock container.
            FormattedText formmatedText = new FormattedText(
                textBlock.Text,
                System.Threading.Thread.CurrentThread.CurrentCulture,
                textBlock.FlowDirection,
               typeface,
                textBlock.FontSize,
                textBlock.Foreground);

            return formmatedText.Width > textBlock.Width;
        }
    }
    • 已標示為解答Marco Zhou 2008年9月3日 上午 09:53
    •  

所有回覆

  • 2008年9月1日 上午 10:55anders06 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Any suggestion?
    anders06
  • 2008年9月2日 上午 06:18Marco Zhou 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    I think you could use the following extension method to determine if a specified TextBlock has been trimmed without calling BuildGeometry() to get the size of the underlying geomtrical metrics which could be different from my personal experience:

    public static class TextBlockExtensions
    {
        public static Boolean IsTextTrimmed(this TextBlock textBlock)
        {
            Typeface typeface = new Typeface(textBlock.FontFamily,
                textBlock.FontStyle,
                textBlock.FontWeight,
                textBlock.FontStretch);

            // FormattedText is used to measure the whole width of the text held up by TextBlock container.
            FormattedText formmatedText = new FormattedText(
                textBlock.Text,
                System.Threading.Thread.CurrentThread.CurrentCulture,
                textBlock.FlowDirection,
               typeface,
                textBlock.FontSize,
                textBlock.Foreground);

            return formmatedText.Width > textBlock.Width;
        }
    }
    • 已標示為解答Marco Zhou 2008年9月3日 上午 09:53
    •  
  • 2008年9月5日 上午 09:20anders06 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Thanks, Marco.
    It works so perfect.

    And it should be "return  formmatedText.Width > textBlock.ActualWidth;“  :) 

    anders06