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;
            }
    
        }
    
  • 相关阅读:
    JS数组定义及详解
    JS中script词法分析
    JS函数 -- 功能,语法,返回值,匿名函数,自调用匿名函数,全局变量与局部变量,arguments的使用
    Java面试(1)-- Java逻辑运算符
    Java面试(3)-- Java关系运算符
    让 history 命令显示日期和时间
    mysql 权限管理
    docker基础
    docker 后台运行和进入后台运行的容器
    expect 自动输入密码
  • 原文地址:https://www.cnblogs.com/maigc249/p/5509853.html
Copyright © 2011-2022 走看看