[WPF]TextTrimming截断后,ToolTip显示完整信息

文本过长被截断后,用ToolTip显示完整信息。

文本未被截断,则不显示ToolTip。

[WPF]TextTrimming截断后,ToolTip显示完整信息  

值转换器:

public class TrimmedTextBlockVisibilityConverter : IValueConverter
{ public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) return Visibility.Collapsed; FrameworkElement textBlock = (FrameworkElement)value; textBlock.Measure(new System.Windows.Size(Double.PositiveInfinity, Double.PositiveInfinity)); if (((FrameworkElement)value).ActualWidth < ((FrameworkElement)value).DesiredSize.Width)
return Visibility.Visible;
else
return Visibility.Collapsed;
} public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

XAML:

<TextBlock TextTrimming="CharacterEllipsis" Text="{Binding SomeTextProperty}">
<TextBlock.ToolTip>
<ToolTip Visibility="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget, Converter={StaticResource trimmedVisibilityConverter}}">
<ToolTip.Content>
<TextBlock Text="{Binding SomeTextProperty}"/>
</ToolTip.Content>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>

参考:http://*.com/questions/6342146/show-tooltip-when-text-is-being-trimmed

上一篇:PHP通过ini_set()来设置显示错误信息和执行时间


下一篇:net发布的dll方法和类显示注释信息(字段说明信息)[图解]