文本过长被截断后,用ToolTip显示完整信息。
文本未被截断,则不显示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://stackoverflow.com/questions/6342146/show-tooltip-when-text-is-being-trimmed
时间: 2024-10-29 19:05:47