Задайте вопросЗадайте вопрос
 

ОтвеченоHow to judge text block is trimming with ...

  • 28 августа 2008 г. 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

Ответы

  • 2 сентября 2008 г. 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 3 сентября 2008 г. 9:53
    •  

Все ответы