原文:WPF 文字换行TextWrapping 显示不全用省略号TextTrimming 显示不全弹提示内容ToolTip

【TextBlock】

换行    TextWrapping="Wrap"

内容显示不全时显示省略号,如 “AAA...”    TextTrimming="CharacterEllipsis" //以单词边界做截断

鼠标提示   <ToolTip>

 

例:  

TextBlock不允许换行,超出后显示省略号截断,超出的情况鼠标移上去会弹出提示内容。

 ①  .xaml

<!--xaml 内容不允许换行,显示不下用省略号-->
<TextBlock TextTrimming="CharacterEllipsis" Width="150" TextWrapping="NoWrap" Text="AAAA">
<TextBlock.ToolTip>
<ToolTip Style="{DynamicResource TooltipStyle}" Content="BBB"/>
</TextBlock.ToolTip>
</TextBlock>

 ② ToolTip样式定义

属性Visibility绑定转换器,仅当内容显示不全时弹出

<Style x:Key="TooltipStyle" TargetType="{x:Type ToolTip}">
<Setter Property="MaxWidth" Value="228"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Background" Value="#FF565656"/>
<Setter Property="FontFamily" Value="{DynamicResource BaseFontFamily}"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="Foreground" Value="#FFFFFFFF"/>
<!--内容显示不全时弹出-->
<Setter Property="Visibility" Value="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget, Converter={StaticResource TrimToolTipConverter}}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToolTip}">
<Border CornerRadius="4" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<TextBlock Padding="8,4" Margin="0,-1,0,0" TextWrapping="Wrap" FontFamily="{TemplateBinding FontFamily}" FontSize="{TemplateBinding FontSize}"
Foreground="{TemplateBinding Foreground}" Text="{TemplateBinding Content}">
</TextBlock>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

 

 ③ Converter转换器

判断TextBlock是否启用Trim属性(内容显示不下),启用了则ToolTip可视

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media; namespace ui.DataConverter
{
/// <summary>
/// 文字显示不下通过ToolTip提示显示
/// </summary>
public class TrimmedTextBlockVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
{
return Visibility.Collapsed;
} TextBlock textBlock = (TextBlock)value;
bool isTrim = IsTextTrimmed(textBlock); if (isTrim)
{
return Visibility.Visible;
}
else
{
return Visibility.Collapsed;
}
} public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
} /// <summary>
/// 判断当前显示的内容是否显示不全被截断
/// </summary>
/// <param name="textBlock"></param>
/// <returns></returns>
private bool IsTextTrimmed(TextBlock textBlock)
{
Typeface typeface = new Typeface(
textBlock.FontFamily,
textBlock.FontStyle,
textBlock.FontWeight,
textBlock.FontStretch); FormattedText formattedText = new FormattedText(
textBlock.Text,
System.Threading.Thread.CurrentThread.CurrentCulture,
textBlock.FlowDirection,
typeface,
textBlock.FontSize,
textBlock.Foreground);
bool isTrimmed = formattedText.Width > textBlock.Width;
return isTrimmed;
}
}
}

 

WPF 文字换行TextWrapping 显示不全用省略号TextTrimming 显示不全弹提示内容ToolTip的更多相关文章

  1. flex布局下,css设置文本不换行时,省略号不显示的解决办法

    大致是有一个main容器是flex布局,左边一个logo固定宽高,右边content动态宽度. <div class="main"> <img alt=" ...

  2. flex 布局下,css 设置文本不换行时,省略号不显示的解决办法

    大致是有一个 main 容器是 flex 布局,左边一个 logo 固定宽高,右边 content 动态宽度. <div class="main"> <img a ...

  3. WPF:验证登录后关闭登录窗口,显示主窗口的解决方法

    http://www.27ba.com/post/145.html WPF:验证登录后关闭登录窗口,显示主窗口的解决方法 最近想做一个基于Socket的通讯工具,想模仿QQ那样,需要先登录,登录成功后 ...

  4. css:在容器内文字超过容器范围,显示一行加省略号或者两行加省略号

    一.显示一行加省略号:各浏览器兼容 .box{ width: 100px; overflow:hidden; white-space:nowrap; text-overflow:ellipsis; } ...

  5. JS控制文字只显示两行,超出部分显示省略号

    由于使用css控制文字只显示多行,超出部分显示省略号,存在一定的兼容性问题,所以总结了一下网上一些大咖使用js实现控制行数的解决方案. 第一步:依次引入jquery.js+jquery.ellipsi ...

  6. WPF的项目,ListBox 纵向滚动条不显示

    最近在做WPF的项目,ListBox 纵向滚动条不显示,但是鼠标滚轮可以在ListBox中使用,但是必须要出现纵向滚动条.  索性就直接在listBox外面包裹一个ScrollViewer. Scro ...

  7. [WPF疑难] 模式窗口被隐藏后重新显示时变成了非模式窗口

    原文:[WPF疑难] 模式窗口被隐藏后重新显示时变成了非模式窗口 [WPF疑难] 模式窗口被隐藏后重新显示时变成了非模式窗口 周银辉 现象: 大家可以试试下面这个很有趣但会带来Defect的现象:当我 ...

  8. WPF 精修篇 窗体唯一(Single) 显示在最前

    原文:WPF 精修篇 窗体唯一(Single) 显示在最前 只运行一个窗体 并在一次点击时 显示到最前 发现用 SetForegroundWindow 并不是稳定的有效 最后使用 SetWindowP ...

  9. css强制换行显示省略号之显示两行后显示省略号

    1,首先来一个固定宽度,在一行显示,超出隐藏,显示省略号的样式 display:block; white-space:nowrap; overflow:hidden; text-overflow:el ...

随机推荐

  1. update进行跨表之间的更新

    有时我们可能须要多个表之间进行更新数据. 我们能够使用这个语句 UPDATE table1,table2 SET table1.column=table2.column, table1.column1 ...

  2. JS实现动画的四条优化方法

    JS实现动画的四条优化方法 1)如果使用的是setTimeout实现的轮询动画,在每一次执行方法之前需要把前面的设置的定时器清除掉 2)为了防止全局变量的污染,我们把定时器的返回值赋值给当前操作元素的 ...

  3. PythonOOP面向对象编程3

    override 函数重写 重写是在自定义的类内添加相应的方法,让自定义的类生成的对象(实例)像内建对象一样进行内建的函数操作 对象转字符串函数重写 repr(obj) 返回一个能代表此对象的表达式字 ...

  4. SpringMVC响应Ajax请求(@Responsebody注解返回页面)

    项目需求描述:page1中的ajax请求Controller,Controller负责将service返回的数据填充到page2中,并将page2整个页面返回到page1中ajax的回调函数. 一句话 ...

  5. 使用Profiles分析SQL语句运行时间和消耗资源

    打开profiling,默认是没开启的. mysql> set profiling=1; 运行要分析的SQL语句 mysql> select count(1) from wechat_em ...

  6. 获取iOS顶部状态栏和Navigation的高度

    状态栏的高度 20 [[UIApplication sharedApplication] statusBarFrame].size.height Navigation的高度 44 self.navig ...

  7. numpy 细节问题

    1. np.expand_dims >> X = np.random.randint(0, 9, (2, 3)) >> mean_X = np.mean(X, axis=0) ...

  8. 算法 Tricks(三)—— 数组(序列)任意区间最小(大)值

    序列(数组)的区间通过左右端点确定,这样首先设置一个最值变量用来记录最值,从左端点一步步移动到右端点,自然移动的过程中也可以计算整个区间的和,也即一次线性遍历下来,可同时获得多个有用信息. // 区间 ...

  9. UVA Bandwidth

    题目例如以下: Bandwidth  Given a graph (V,E) where V is a set of nodes and E is a set of arcsin VxV, and a ...

  10. js 第四章 cookie的操作

    js 第四章 cookie的操作 一.学习要点 掌握cookie的简单应用 二. js 第四章 cookie的操作 了解cookie 什么是cookie? cookie 是存储于访问者的计算机中的变量 ...