This article is compatible with the latest version of Silverlight.
New information about the tooltip can be found here.
Introduction
The theme of this article is the ToolTip control. It can be very useful as it enhances our application’s user interface.
Using the ToolTip Control
The ToolTipService class must be used along with the ToolTip control in order to display a tooltip to our control. For the example I will use an image control, but the following can be applied to each of the controls. Here is the code:
<Image Source="GiantSeaTurtle.jpg" Width="400" Height="300">
<ToolTipService.ToolTip>
<ToolTip Content="Turtle"></ToolTip>
</ToolTipService.ToolTip>
</Image>
And it works:
The ToolTip control also provides some formatting properties such as Background, FontSize, FontWeight, Foreground etc. The VerticalOffset and HorizontalOffset adjust the position of the tooltip in relation to the mouse. Here is an example:
<Image Source="GiantSeaTurtle.jpg" Width="400" Height="300" >
<ToolTipService.ToolTip>
<ToolTip Content="Turtle" FontWeight="Bold" VerticalOffset="10"
HorizontalOffset="10" >
<ToolTip.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White"
Offset="0"></GradientStop>
<GradientStop Color="DarkCyan"
Offset="0.8"></GradientStop>
</LinearGradientBrush>
</ToolTip.Background>
</ToolTip>
</ToolTipService.ToolTip>
</Image>
Another very important thing about the ToolTip control is that it doesn’t receive focus. Also when I tried to put a Button in the content of the ToolTip control the Click event was not raised and the button animation froze. I don’t know whether these two things are related, but if you want to add some interactivity to your tooltip keep them in mind.
Here is a live demo of using the ToolTip control with Image control nested in it.
<Image x:Name="turtle" Source="GiantSeaTurtle.jpg" Width="400" Height="300">
<ToolTipService.ToolTip>
<ToolTip>
<ToolTip.Content>
<Image Source="GiantSeaTurtle.jpg" Width="80"
Height="60"></Image>
</ToolTip.Content>
</ToolTip>
</ToolTipService.ToolTip>
</Image>
FAQ
· How can I attach a tooltip to my control?
A tooltip can be attached using the ToolTip attached property of the ToolTipService class and the ToolTip control.
· Can I nest other controls in the ToolTip control?
Consider the ToolTip control as a panel – you can put other controls in it (stack panel, images, text blocks etc.).
· Can I format the content of the ToolTip control?
Yes, you can. The ToolTip Control provides properties for text formatting and background modifying (Background, Foreground, FontFamily, FontSize etc.).
· Can I nest a Button in the ToolTip control and use its Click event?
As far as I tried – no. The Click event is not raised and the animation freezes on button down. This applies not only to the Button controls and their Click event, but to the other controls too. I recommend using static controls such as the Image control, the StackPanel, the TextBlock and etc. when nesting something in the ToolTip control.
Summary
Now it’s clear how to use the ToolTipService. You may consider the ToolTipService to be beneficial for your application, because, as we saw, it provides numerous styling options thanks to the ToolTip control and enhances the UI of our Silverlight application.
Conclusion
This article is just a brief tutorial that includes the key features of the ToolTip control. It targets the developer who has just started with the Silverlight controls. Any comments are welcome.