Hide tooltip if binding is null
Asked Answered
L

7

44

Currently i've got the following code to show a tooltip.

<Border BorderBrush="Black"
        BorderThickness="{Binding Border}"
        Height="23"
        Background="{Binding Color}">
<ToolTipService.ToolTip>
    <TextBlock Text="{Binding TooltipInformation}" />
</ToolTipService.ToolTip>

This is presented in a ItemsControl with about 25 items. Only a few of these have a value set to TooltipInformation

If TooltipInforation is an empty string, it still shows the tooltipbox containing the textblock as a very small window (about 5px high and 20px wide). Even if I set the textblock visbility to collapsed.

Is there a way to completely remove the tooltip if the value of TooltipInformation is null or a empty string?

Lease answered 6/5, 2011 at 11:51 Comment(0)
G
17

One way you can do that is wrap the ToolTip in a Rectangle and give it a Transparent color. Then you just set the Visibility to Collapsed on this Rectangle.

Update:

<Border Background="#FFE45F5F">
    <Grid>
        <TextBlock Text="{Binding Property1}"/>
        <Rectangle Fill="Transparent" Visibility="{Binding Property2, Converter={StaticResource BooleanToVisibilityConverter}}" ToolTipService.ToolTip="{Binding TooltipInformation}"/>
    </Grid>
</Border>
Graff answered 6/5, 2011 at 12:5 Comment(2)
Tried wrapping a new border, with the tooltip service inside it and the textblock in a stackpanel. Then add binding to a Boolean which checks whether the string is empty or not. Have that boolean bound to the new border's visbility with a booltovisibile converter. But it didn't do anythnig. Infact, it disabled every tooltip :)Lease
Hi, please see my updated answer. You actually don't need another Panel, just use a Rectangle to trigger the hide/show of the ToolTip. :)Graff
A
127

One way to hide an empty tooltip for all controls is to create a style in a resource dictionary that is included in your App.xaml. This style sets the visibility to collapsed when the tooltip is an empty string or null:

<!-- Style to hide tool tips that have an empty content. -->
<Style TargetType="ToolTip">
    <Style.Triggers>
        <Trigger Property="Content"
                 Value="{x:Static sys:String.Empty}">
            <Setter Property="Visibility"
                    Value="Collapsed" />
        </Trigger>
        <Trigger Property="Content"
                 Value="{x:Null}">
            <Setter Property="Visibility"
                    Value="Collapsed" />
        </Trigger>
    </Style.Triggers>
</Style>

Also include sys namespace (for String.Empty):

xmlns:sys="clr-namespace:System;assembly=mscorlib"
Antibiotic answered 16/12, 2011 at 10:11 Comment(6)
perfect. I would give you significantly more than +1 if I couldAndrej
there are no triggers in silverlight.Schizo
This is the best answer.Aesthetic
Does not work if Content is a UI element, e.g. a TextBlock.Steffin
add BasedOn="{StaticResource {x:Type ToolTip}} to avoid breaking existing styles (e.g. from themes)Iloilo
Like @Steffin says, this only works if Content is a text. You have to use <ToolTipService Tooltip="{Binding TooltipInformation}" /> and not <ToolTipService.Tooltip><TextBlock Text="{Binding TooltipInformation}" /></ToolTipService.Tooltip>Bespangle
G
17

One way you can do that is wrap the ToolTip in a Rectangle and give it a Transparent color. Then you just set the Visibility to Collapsed on this Rectangle.

Update:

<Border Background="#FFE45F5F">
    <Grid>
        <TextBlock Text="{Binding Property1}"/>
        <Rectangle Fill="Transparent" Visibility="{Binding Property2, Converter={StaticResource BooleanToVisibilityConverter}}" ToolTipService.ToolTip="{Binding TooltipInformation}"/>
    </Grid>
</Border>
Graff answered 6/5, 2011 at 12:5 Comment(2)
Tried wrapping a new border, with the tooltip service inside it and the textblock in a stackpanel. Then add binding to a Boolean which checks whether the string is empty or not. Have that boolean bound to the new border's visbility with a booltovisibile converter. But it didn't do anythnig. Infact, it disabled every tooltip :)Lease
Hi, please see my updated answer. You actually don't need another Panel, just use a Rectangle to trigger the hide/show of the ToolTip. :)Graff
R
14

This is a WPF answer (haven't tried it in Silverlight).

Use ToolTipService.IsEnabled, and bind it to the tooltip property. Then use a converter to convert the tooltip string to a bool.

For example, I have the following:

<TextBlock x:Name="textBlock" ToolTipService.IsEnabled="{Binding EntryToolTip, Converter={StaticResource StringNullOrEmptyToBoolConverter}}">
...
</TextBlock>

Or in code-behind

ToolTipService.SetIsEnabled(textBlock, false);
Rosabelle answered 25/9, 2017 at 12:26 Comment(1)
+1 This worked for me, and fits better in our application. We have potentially dozens of tool tips across many different types of controls, so styling would still be redundant. Further we have specialized tooltips in some places (but not others). This approach allows for a single way to say "get rid of blanks" we can use everywhere.Rees
O
4

I was having the same issue as I was setting value to String.Empty. Setting it to null solves the problem.

WinRT/Windows 8 App XAML

Ockeghem answered 29/12, 2014 at 11:50 Comment(0)
E
1

If just using the default tooltip I would otherwise recommend either setting the bound value to null in the viewmodel or using a converter whenever the item is empty.

In my case I've got a:

public string Name { get; }

Bound using:

<TextBlock Text="{Binding Name}" TextTrimming="CharacterEllipsis" Tooltip="{Binding Name}" />

Where the idea is to show the full name in the tooltip if cut of due to lack of width. In my viewmodel I simply:

if (string.IsNullOrEmpty(Name)) Name = null;

At least in .Net 4.0 this will not show a tooltip for me.

Editheditha answered 26/8, 2014 at 13:50 Comment(0)
H
0

Strangely, none of these answers worked in my case. A reply to the top answer alludes to it - if you're ToolTip is related to a TextBlock then that solution won't work. I have a TextBlock within a DataGridTemplateColumn.CellTemplate element, and I just bound the text directly to the ToolTip property of the TextBlock like this:

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBlock
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            ToolTip="{Binding Path=SomeTextProperty}"
            Style="{StaticResource TextBlockOverflowStyle}"
            Text="{Binding Path=SomeTextProperty, NotifyOnSourceUpdated=True}" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

And I ended up getting the desired behavior (hidden tooltip when text is empty) "for free".

Hectic answered 28/5, 2022 at 1:51 Comment(0)
C
-2

You could create a converter from string to bool that returns false if the string length is 0 and true otherwise, then bind ToolTip.Active to TooltipInformation with that converter.

Cavefish answered 6/5, 2011 at 12:9 Comment(1)
ToolTip.Active is not availbie in current context.Lease

© 2022 - 2024 — McMap. All rights reserved.