Show Tooltip when text is being trimmed

前端 未结 6 1248
北海茫月
北海茫月 2020-12-30 02:45
  
     
        

        
6条回答
  •  既然无缘
    2020-12-30 03:46

    I found the simplest solution to extend TextBlock and compare text lengths to determine whether to show tooltip, i.e.,

    public class ToolTipTextBlock : TextBlock
       {
          protected override void OnToolTipOpening(ToolTipEventArgs e)
          {
             if (TextTrimming != TextTrimming.None)
             {
               e.Handled = !IsTextTrimmed();
             }
          }
    
          private bool IsTextTrimmed()
          {
             var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
             var formattedText = new FormattedText(Text, CultureInfo.CurrentCulture, FlowDirection, typeface, FontSize, Foreground);
             return formattedText.Width > ActualWidth;
          }
       }
    

    Then simply use this custom text block in xaml as follows:

    
    

提交回复
热议问题