MSDN > Home page del forum > Windows Presentation Foundation (WPF) > How to judge text block is trimming with ...
Formula una domandaFormula una domanda
 

Con rispostaHow to judge text block is trimming with ...

  • giovedì 28 agosto 2008 8.29anders06 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Contiene codice
    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

Risposte

  • martedì 2 settembre 2008 6.18Marco Zhou Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    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;
        }
    }
    • Contrassegnato come rispostaMarco Zhou mercoledì 3 settembre 2008 9.53
    •  

Tutte le risposte

  • lunedì 1 settembre 2008 10.55anders06 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Any suggestion?
    anders06
  • martedì 2 settembre 2008 6.18Marco Zhou Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    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;
        }
    }
    • Contrassegnato come rispostaMarco Zhou mercoledì 3 settembre 2008 9.53
    •  
  • venerdì 5 settembre 2008 9.20anders06 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Thanks, Marco.
    It works so perfect.

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

    anders06