I had a small issue using Alex answer and had to change my logic a bit to clarify if the text in the text block was being trimmed.
var formattedText = new FormattedText(
Text, System.Threading.Thread.CurrentThread.CurrentCulture, FlowDirection, typeface, FontSize,
Foreground, VisualTreeHelper.GetDpi( this ).PixelsPerDip ) { MaxTextWidth = ActualWidth };
//Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
return ( Math.Floor(formattedText.Height ) > ActualHeight || Math.Floor( formattedText.MinWidth ) > ActualWidth;
This works perfectly for me.
I defined a user control that was a TextBlock with ellipsis enabled. Then I defined 2 functions for OnMouseUp and OnMouseDown, so that when the user clicked on the textblock that had overflow it would display a tooltip with the full value.
This is the OnMouseDown function
private void TextBlockWithToolTipView_OnMouseDown(
object sender,
MouseButtonEventArgs e )
{
var typeface = new Typeface(
FontFamily,
FontStyle,
FontWeight,
FontStretch);
var formattedText = new FormattedText(
Text, System.Threading.Thread.CurrentThread.CurrentCulture, FlowDirection, typeface, FontSize,
Foreground, VisualTreeHelper.GetDpi( this ).PixelsPerDip ) { MaxTextWidth = ActualWidth };
if (Math.Floor(formattedText.Height) > ActualHeight || Math.Floor(formattedText.MinWidth) > ActualWidth )
{
if( ToolTip is ToolTip tt )
{
{
if( tt.PlacementTarget == null )
{
tt.PlacementTarget = this;
}
tt.IsOpen = true;
e.Handled = true;
}
}
}
}
And this was the Xaml bit
<TextBlock
ToolTipService.IsEnabled="True"
MouseDown="TextBlockWithToolTipView_OnMouseDown"
MouseLeave="TextBlockWithToolTipView_OnMouseLeave"
TextTrimming="CharacterEllipsis"
TextWrapping="WrapWithOverflow">
<TextBlock.ToolTip>
<ToolTip
DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}">
<TextBlock Text="{Binding Path=Text, Mode=OneWay }"
TextWrapping="Wrap"/>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>