ToolTip小型信息弹窗

ToolTip小型信息弹窗

[!IMPORTANT]

很多时候,如果TextBlock能显示全的时候,我们不希望显示ToolTip,只有当TextBlock显示不全的时候,使用…分栏符,鼠标悬停在该TextBlock上,ToolTip显示内容,接下来我分为两个模块(常规显示、进阶显示)为大家演示,并附上了全部演示代码

ToolTip的常规显示

(1)前端代码1

<TextBlock Margin="20" Width="150" Height="26" Foreground="#FFFFFF" FontSize="14" Text="测试测试测试测试测试测试测试ToolTip" ToolTip="测试测试测试测试测试测试测试ToolTip"></TextBlock>

(2)运行效果1

请添加图片描述

(3)前端代码2


<TextBlock Margin="20" Width="150" Height="26" Foreground="#FFFFFF" FontSize="14" Text="测试ToolTip" ToolTip="测试ToolTip"></TextBlock>

(4)运行效果2

请添加图片描述

ToolTip是进阶显示

(1)自定义TextBlockToolTip控件,直接上完整的代码

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace TestToolTip
{
    public class TextBlockToolTip : Grid
    {
        public static bool GetAutoTooltip(DependencyObject obj)
        {
            return (bool)obj.GetValue(AutoTooltipProperty);
        }

        public static void SetAutoTooltip(DependencyObject obj, bool value)
        {
            obj.SetValue(AutoTooltipProperty, value);
        }
        // Using a DependencyProperty as the backing store for AutoTooltip.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AutoTooltipProperty =
            DependencyProperty.RegisterAttached("AutoTooltip", typeof(bool), typeof(TextBlockToolTip), new PropertyMetadata(false, OnAutoTooltipPropertyChanged));
        private static void OnAutoTooltipPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TextBlock textBlock = d as TextBlock;
            if (textBlock == null)
                return;
            if (e.NewValue.Equals(true))
            {
                textBlock.TextTrimming = TextTrimming.WordEllipsis;
                ComputeAutoTooltip(textBlock);
                textBlock.SizeChanged += TextBlock_SizeChanged;
            }
            else
            {
                textBlock.SizeChanged -= TextBlock_SizeChanged;
            }
        }

        private static void TextBlock_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            TextBlock textBlock = sender as TextBlock;
            ComputeAutoTooltip(textBlock);
        }

        private static void ComputeAutoTooltip(TextBlock textBlock)
        {
            if (textBlock.ActualWidth < MeasureTextBlockWidth(textBlock))
            {
                //创建ToolTip
                ToolTip toolTip = GenToolTip(textBlock.Text);
                ToolTipService.SetToolTip(textBlock, toolTip);
            }
            else
            {
                ToolTipService.SetToolTip(textBlock, null);
            }
        }

        /// <summary>
        /// 生成ToolTip,这里的ToolTip样式可以根据自己的需求修改
        /// </summary>
        private static ToolTip GenToolTip(string text)
        {
            //ToolTip
            ToolTip toolTip = new ToolTip();
            //TextBlock
            FrameworkElementFactory textBlockFactory = new FrameworkElementFactory(typeof(TextBlock));
            textBlockFactory.SetValue(TextBlock.BackgroundProperty, Brushes.Transparent);
            textBlockFactory.SetValue(TextBlock.ForegroundProperty, Brushes.White);
            textBlockFactory.SetValue(TextBlock.TextWrappingProperty, TextWrapping.Wrap);
            textBlockFactory.SetValue(TextBlock.LineHeightProperty, 17.0);
            textBlockFactory.SetValue(TextBlock.FontSizeProperty, 12.0);
            textBlockFactory.SetValue(TextBlock.MaxWidthProperty, 300.0);
            textBlockFactory.SetValue(TextBlock.TextProperty, text);
            //Border
            FrameworkElementFactory borderFactory = new FrameworkElementFactory(typeof(Border));
            borderFactory.SetValue(Border.BackgroundProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#0A0A0A")));
            borderFactory.SetValue(Border.CornerRadiusProperty, new CornerRadius(2));
            borderFactory.SetValue(Border.PaddingProperty, new Thickness(8, 5, 11, 6));
            borderFactory.SetValue(Border.BorderBrushProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4C4C4C")));
            borderFactory.SetValue(Border.BorderThicknessProperty, new Thickness(1));
            borderFactory.AppendChild(textBlockFactory);
            //ControlTemplate
            ControlTemplate controlTemplate = new ControlTemplate(typeof(ToolTip));
            controlTemplate.VisualTree = borderFactory;
            toolTip.Template = controlTemplate;
            return toolTip;
        }

        /// <summary>
        /// 测量TextBlock所需实际长度
        /// </summary>
        private static double MeasureTextBlockWidth(TextBlock textBlock)
        {
            //TextBlock自带测量方法
            TextBlock measureBlock = new TextBlock { Text = textBlock.Text, FontSize = textBlock.FontSize, FontFamily = textBlock.FontFamily, FontStyle = textBlock.FontStyle, FontWeight = textBlock.FontWeight, FontStretch = textBlock.FontStretch };
            measureBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
            return measureBlock.DesiredSize.Width;
        }
    }
}

(2)测试程序


<TextBlock  Margin="20" Width="150" Height="26" Foreground="#FFFFFF" FontSize="14" Text="测试ToolTip" ToolTip="测试ToolTip" local:TextBlockToolTip.AutoTooltip="True"></TextBlock>
<TextBlock Margin="20,80,20,20" Width="150" Height="26" Foreground="#FFFFFF" FontSize="14" Text="测试测试测试测试测试测试测试测试测试测试测试测试测试测试ToolTip" ToolTip="测试测试测试测试测试测试测试测试测试测试测试测试测试测试ToolTip" local:TextBlockToolTip.AutoTooltip="True"></TextBlock>

(3)运行效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值