WPF--TextBlock的ToolTip附加属性
大家可能在项目中,有的时候,由于显示的内容过长,所以,需要显示一部分内容,然后后面用省略号,把鼠标放上去,会显示出来全部的内容。
作为一个LowB程序员的我,第一反应是SubString截取,然后替换,然后ToolTip显示原有的内容。
我相信很大一部分的初级程序员第一想法也是这个,然而,这种方法不具有一个通用型,当然,有的童鞋可能说,写个函数,然后设置传递的参数,这样不就可以了吗?
事实上,这样也是很不方便的。
本文的重点来了:
昨天在项目里发现了同事写的一个很棒的附加属性,今天把它共享出来,供大家学习。
在此,先感谢那位同事,虽然不知道谁写的(没有备注),但是真的很棒。
代码不难,但是效果确实不错,先看下效果。
由于是一个可变的长度,所以,不是用传统的一个截取的方法,而是根据TextBlock外部的宽度,自动实现的截取效果,而且感觉系统的这种,还挺有意思。
可以看到第一行,前面是文字,后面是数字“我是测试长度1111111111111111111111111”,直接从数字部分往后,都用了省略号,而不是必须到了最外层的边缘才进行的截取。
第二行,由于后面是文字,到了最外层的宽度,才用省略号进行了替换。
代码如下:
- public class TextBlockToolTip
- {
- 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)
- {
- textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
- var width = textBlock.DesiredSize.Width;
- if (textBlock.ActualWidth<width)
- {
- ToolTipService.SetToolTip(textBlock, textBlock.Text);
- }
- else
- {
- ToolTipService.SetToolTip(textBlock, null);
- }
- }
- }
测试代码如下:
就是把附加属性AutoToolTip设置为True就可以了。
希望这个附加属性可以对你们有所帮助。
WPF--TextBlock的ToolTip附加属性的更多相关文章
- WPF TextBlock 判断 isTextTrimmed 文本是否超出
WPF TextBlock 设置TextTrimming情况下 判断 isTextTrimmed(Text 文本是否超出 是否出现了省略号) private bool HasTextTrimmed(T ...
- WPF TextBlock IsTextTrimmed 判断文本是否超出
WPF TextBlock/TextBox 设置TextTrimming情况下 判断 isTextTrimmed(Text 文本是否超出 是否出现了省略号) private bool IsTextTr ...
- WPF 高级篇 MVVM 附加属性
原文:WPF 高级篇 MVVM 附加属性 WPF 特性之一 附加属性 在本文里实现文本框内容的验证 public class TextBoxHelper:DependencyObject { publ ...
- wpf textblock超出显示范围后显示tooltip
public static class TextTrmmingShowToolTip { public static readonly DependencyProperty IsToolTipProp ...
- WPF QuickStart系列之附加属性(Attached Property)
这一篇博客是关于如何使用附加属性和创建自定义附加属性的. 1. 附加属性使用, WPF中对附加属性使用最多的莫过于对控件布局时设置控件的位置,例如在Canvas中有一个Rectangle, Ellip ...
- WPF TextBlock 绑定 换行
最近有个小需求 需要在textblock中换行 其实textblock换行有很多写法,比如: Xaml: <TextBlock Text="AAAAA BBBBB" /> ...
- WPF 依赖属性和附加属性
依赖属性: 依赖属性就是自己没有值,通过Binding从数据源获得值,就是依赖在别人身上,拥有依赖属性的对象称为依赖对象. 依赖属性的值存在哪里? 在WPF运行时,维护了一个全局的Hashtable存 ...
- WPF TextBlock文子超出在最后加上省略号
加上这个属性:TextTrimming="CharacterEllipsis" <TextBlock Text="{Binding filepaths}" ...
- 给 TextBlock 加 ToolTip
<TextBlock ToolTip="{Binding RelativeSource={RelativeSource Self},Path=Text}" Text=&quo ...
随机推荐
- 统计dir_path下所有文件类型的文件数量
#!/bin/bash #!文件名为countfile.sh ]; then echo "Usage is $0 basepath"; exit fi path=$ declare ...
- ajax请求返回数据,模板中的数据处理
/*ajax请求返回数据,模板中的数据处理*/ function QueryGameAsset(){ var new_start_time=$('#new_start_time').val();//开 ...
- 【LeetCode】190. Reverse Bits
题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
- 8.Smarty的条件判断语句的写法
{if $newObj eq 'a'} welcome a {elseif $a eq 'b'} welcome b {else} welcome others {/if}
- C#注册表操作类--完整优化版
using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; namespace ...
- JavaScript从入门到忘记
JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. 一.如何编写 二.变 ...
- SpringEL 表达式错误记录
原因暂时未知....
- raft如何实现Linearizable Read
Linearizable Read通俗来讲,就是读请求需要读到最新的已经commit的数据,不会读到老数据. 对于使用raft协议来保证多副本强一致的系统中,读写请求都可以通过走一次raft协议来满足 ...
- java循环、数组练习
System.out.println("请输入学生个数"); int a=sc.nextInt();//定义一个变量说明学生的数量 int max=0; int[] scores= ...
- pgsql 递归查询 分页
--向下查询 WITH RECURSIVE res AS ( union ALL SELECT t_tree.* FROM t_tree, res WHERE t_tree.pid = res.id ...