原文: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. 无法显式调用运算符或访问器 错误处理方法 DLL study

    无法显式调用运算符或访问器 错误处理方法 转 无法显式调用运算符或访问器 错误处理方法 反汇编一个dll类库,导出的项目会报出很多bug,其中主要的就是“无法显式调用运算符或访问器”这个错误,看了一下 ...

  2. ajax的post请求与编码

    window.onload = function(){ document.getElementById('username').onblur = function(){ var name = docu ...

  3. Android Studio - no debuggable applications 的解决的方法

    之前logcat总是无法显示调试应用的信息 曾经我都是卸载重装.后来发如今StackOverflow有一个哥们说的非常对.一次就成功. 原话是这么说的: You also should have To ...

  4. oracle exp 备份脚本

    #!/bin/bash#Oracle 环境变量 NLS_LANG=AMERICAN_AMERICA.AL32UTF8 ORACLE_SID=zgw ORACLE_BASE=/opt/oracle OR ...

  5. vim 保存文件的回车换行模式

    设置模式:unix,dos :set fileformat=unix  fileforman可以直接缩写为ff

  6. Linux 下配置 Python IDE——Emacs

    工欲善其事,必先利其器.Python作为高级语言,因为其简介.灵活已经被越来越多的程序员所青睐.在尝试了众多IDE之后,终于找到了自己的挚爱.废话少说,下面开始说一下如何在linux下安装配置Emac ...

  7. 超链接a的download属性 实现文件下载功能

    今天做项目遇到一个要点击按钮下载文件的功能. 百度之 知道了a的download属性.这是HTML5的新特性.主要功能是实现下载功能.主要语法是 <a href="url" ...

  8. AIR 初步 Javascript学习之cookie操作

    //设置cookie的名称,值,过期时间         function setCookie(cookieName,cookieValue,cookieExpire) {             v ...

  9. [PostgreSQL] Ensure Uniqueness in Postgres

    Let’s say we have a bank. Our bank wants to give each account for each user a unique name, for insta ...

  10. 数据类型总结——Boolean类型(布尔类型)

    相关文章 简书原文:https://www.jianshu.com/p/e5c75d4be636 数据类型总结——概述:https://www.cnblogs.com/shcrk/p/9266015. ...