3

I have bound the Tooltip property of a control in wpf to a string called TooltipText . TooltipText default value is empty string "". It gets populated later on under some conditions.

The problem is when the TooltipText is empty it looks odd when the user does mouse over my control as it displays an empty box tooltip.

Is there a way to NOT SHOW the tooltip when TooltipText is empty but show it when its length is greater than 1? I hope I made myself clear.

I do this in xaml as (code is incomplete and only partial):

<c:MyControl ToolTip="{Binding ElementName=controlName, Path=TooltipText}">
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742

2 Answers2

10

Set the property to null instead of "".

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

Excellent answer on this topic already. Works like a charm!

Copied the relevant code here for posterity.

<TextBlock Text="{Binding Text}"
     IsMouseDirectlyOverChanged="TextBlock_IsMouseDirectlyOverChanged" >
     <TextBlock.ToolTip>
     <ToolTip Visibility="Collapsed">
         <TextBlock Text="{Binding Text}"></TextBlock>
     </ToolTip>
     </TextBlock.ToolTip>
</TextBlock>

Code behind:

private void TextBlock_IsMouseDirectlyOverChanged(object sender, e)
{
    bool isMouseOver = (bool)e.NewValue;
    if (!isMouseOver)
        return;
    TextBlock textBlock = (TextBlock)sender;
    bool needed = textBlock.ActualWidth > 
        (this.listView.View as GridView).Columns[2].ActualWidth;
    ((ToolTip)textBlock.ToolTip).Visibility = 
        needed ? Visibility.Visible : Visibility.Collapsed;
}
Community
  • 1
  • 1
phillip
  • 2,618
  • 19
  • 22
  • Thanks for the reply...but this looks like too much code to achieve what I want....Also setting the default value to null works! – guest123456789 Dec 08 '10 at 17:28