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?
One way you can do that is wrap the
ToolTip
in aRectangle
and give it aTransparent
color. Then you just set theVisibility
toCollapsed
on thisRectangle
.Update:
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
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:
Also include sys namespace (for String.Empty):
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:
Or in code-behind
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.
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:
Bound using:
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:
At least in .Net 4.0 this will not show a tooltip for me.