Hide tooltip if binding is null

Theun Arbeider picture Theun Arbeider · May 6, 2011 · Viewed 21.7k times · Source

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?

Answer

HeWillem picture HeWillem · Dec 16, 2011

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"