WPF 文字换行TextWrapping 显示不全用省略号TextTrimming 显示不全弹提示内容ToolTip
原文: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的更多相关文章
- flex布局下,css设置文本不换行时,省略号不显示的解决办法
大致是有一个main容器是flex布局,左边一个logo固定宽高,右边content动态宽度. <div class="main"> <img alt=" ...
- flex 布局下,css 设置文本不换行时,省略号不显示的解决办法
大致是有一个 main 容器是 flex 布局,左边一个 logo 固定宽高,右边 content 动态宽度. <div class="main"> <img a ...
- WPF:验证登录后关闭登录窗口,显示主窗口的解决方法
http://www.27ba.com/post/145.html WPF:验证登录后关闭登录窗口,显示主窗口的解决方法 最近想做一个基于Socket的通讯工具,想模仿QQ那样,需要先登录,登录成功后 ...
- css:在容器内文字超过容器范围,显示一行加省略号或者两行加省略号
一.显示一行加省略号:各浏览器兼容 .box{ width: 100px; overflow:hidden; white-space:nowrap; text-overflow:ellipsis; } ...
- JS控制文字只显示两行,超出部分显示省略号
由于使用css控制文字只显示多行,超出部分显示省略号,存在一定的兼容性问题,所以总结了一下网上一些大咖使用js实现控制行数的解决方案. 第一步:依次引入jquery.js+jquery.ellipsi ...
- WPF的项目,ListBox 纵向滚动条不显示
最近在做WPF的项目,ListBox 纵向滚动条不显示,但是鼠标滚轮可以在ListBox中使用,但是必须要出现纵向滚动条. 索性就直接在listBox外面包裹一个ScrollViewer. Scro ...
- [WPF疑难] 模式窗口被隐藏后重新显示时变成了非模式窗口
原文:[WPF疑难] 模式窗口被隐藏后重新显示时变成了非模式窗口 [WPF疑难] 模式窗口被隐藏后重新显示时变成了非模式窗口 周银辉 现象: 大家可以试试下面这个很有趣但会带来Defect的现象:当我 ...
- WPF 精修篇 窗体唯一(Single) 显示在最前
原文:WPF 精修篇 窗体唯一(Single) 显示在最前 只运行一个窗体 并在一次点击时 显示到最前 发现用 SetForegroundWindow 并不是稳定的有效 最后使用 SetWindowP ...
- css强制换行显示省略号之显示两行后显示省略号
1,首先来一个固定宽度,在一行显示,超出隐藏,显示省略号的样式 display:block; white-space:nowrap; overflow:hidden; text-overflow:el ...
随机推荐
- 理解OAuth 2.0 - 阮一峰的网络日志
原文:理解OAuth 2.0 - 阮一峰的网络日志 理解OAuth 2.0 作者: 阮一峰 日期: 2014年5月12日 OAuth是一个关于授权(authorization)的开放网络标准,在全世界 ...
- ZOJ 1242 Carbon Dating
UVA昨天上不去,今天一大早起来还是上不去 0.0 于是去ZOJ 这题大意就是半衰期... 取对数用到了换底公式...我都忘了这玩意了T T 上代码... #include<iostream&g ...
- Android内存优化(使用SparseArray和ArrayMap取代HashMap)
在Android开发时,我们使用的大部分都是Java的api,比方HashMap这个api,使用率非常高,可是对于Android这样的对内存非常敏感的移动平台,非常多时候使用一些java的api并不能 ...
- CSS3实现的立体button
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- asp.net获取客户真实ip非代理ip:
public string GetUserIP() { string _userIP; if(Request.ServerVariables["HTTP_VIA& ...
- flask的使用(一)
1.程序基本的说明 #-*-encoding=utf--*- 从flask中引入类 from flask import Flask ,render_template import config 初始化 ...
- poj 3100 (zoj 2818)||ZOJ 2829 ||ZOJ 1938 (poj 2249)
水题三题: 1.给你B和N,求个整数A使得A^n最接近B 2. 输出第N个能被3或者5整除的数 3.给你整数n和k,让你求组合数c(n,k) 1.poj 3100 (zoj 2818) Root of ...
- POJ 1364 King (UVA 515) 差分约束
http://poj.org/problem?id=1364 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemi ...
- POJ 1932 XYZZY (ZOJ 1935)SPFA+floyd
http://poj.org/problem?id=1932 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1935 题目大 ...
- Textillate.js有什么用及使用实例
Textillate.js有什么用及使用实例 一.总结 一句话总结:Textillate.js – 实现动感的 CSS3 文本动画的简单插件 二.textillate.js 文字动画 textilla ...