zoukankan      html  css  js  c++  java
  • TextBlock AutoTooltip

    这里实现了一个简单的TextBlock的AutoTooltip,Code as Follow:

    public sealed class SimpleTextBlockTrimming
        {
            public static bool GetAutoTooltip( DependencyObject obj)
            {
                return ( bool)obj.GetValue(AutoTooltipProperty);
            }
            
            public static void SetAutoTooltip( DependencyObject obj, bool value)
            {
                obj.SetValue(AutoTooltipProperty, value);
            }
    
            public static readonly DependencyProperty AutoTooltipProperty =
                DependencyProperty.RegisterAttached(
                    "AutoTooltip",
                    typeof( bool),
                    typeof( SimpleTextBlockTrimming),
                    new PropertyMetadata(false , OnAutoTooltipPropertyChanged));
    
            private static void OnAutoTooltipPropertyChanged( DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var textBlock = d as TextBlock;
                if (textBlock == null)
                {
                    return;
                }
    
                if (e.NewValue.Equals( true))
                {
                    textBlock.TextTrimming = TextTrimming.CharacterEllipsis;
                    textBlock.MouseEnter += textBlock_MouseEnter;
                }
                else
                {
                    textBlock.MouseEnter -= textBlock_MouseEnter;
                }
            }
    
            private static void textBlock_MouseEnter( object sender, MouseEventArgs e)
            {
                var tblock = sender as TextBlock;
                if ( null == tblock)
                    return;
    
                ToolTipService.SetToolTip(tblock, IsTextTrimmed(tblock) ? tblock.Text : null);
            }
    
            private static bool IsTextTrimmed( 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 formattedText = new FormattedText (
                    textBlock.Text,
                    System.Threading. Thread.CurrentThread.CurrentCulture,
                    textBlock.FlowDirection,
                    typeface,
                    textBlock.FontSize,
                    textBlock.Foreground);
    
                return formattedText.Width - textBlock.ActualWidth > 2;
            }
    
        }
    
  • 相关阅读:
    【校招面试 之 C/C++】第1题 为什么优先使用构造函数的初始化列表
    Linux平台ORACLE INSTANT客户端安装
    ORACLE数据库链接
    ORACLE用户管理
    【转】软件开发工具介绍之 6.Web开发工具
    【转】三大UML建模工具Visio、Rational Rose、PowerDesign的区别
    ORACLE数据库查看执行计划
    数据分析方法
    ORACLE对象大小写问题
    计算机改名引发的ORA
  • 原文地址:https://www.cnblogs.com/maigc249/p/5509853.html
Copyright © 2011-2022 走看看