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)运行效果