Microsoft Developer Network > 포럼 홈 > Windows Presentation Foundation (WPF) > How to judge text block is trimming with ...
질문하기질문하기
 

답변됨How to judge text block is trimming with ...

  • 2008년 8월 28일 목요일 오전 8: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일 화요일 오전 6: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일 수요일 오전 9:53
    •  

모든 응답

  • 2008년 9월 1일 월요일 오전 10:55anders06 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Any suggestion?
    anders06
  • 2008년 9월 2일 화요일 오전 6: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일 수요일 오전 9:53
    •  
  • 2008년 9월 5일 금요일 오전 9:20anders06 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Thanks, Marco.
    It works so perfect.

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

    anders06