原文: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;
}
}
}
?
原文地址:https://www.cnblogs.com/lonelyxmas/p/10247265.html
时间: 2024-10-27 18:33:30