Welcome to MSDN Blogs Sign in | Join | Help

Customizing a ToolTip

(Special thanks to Andre Michaud and Mike Russell who showed me how to do this.)

ToolTips are quite useful for for displaying helpful information when the user hovers over a control. The way that ToolTips are usually used is by setting the ToolTipService.ToolTip property to some text. This will display the text in a rather bland rectangle, without wrapping. Here are some ways to make your ToolTips more useful and nicer looking.

ToolTip are typically defined like this:

<TextBox Height="20" Width="100" ToolTipService.ToolTip="Some helpful text"/>

The standard ToolTip looks like this:

image

ToolTipService.ToolTip is an attached property that defines the Content of the ToolTip. It will get put into a ContentPresenter. In this case, since it is a string, the ContentPresenter will create a TextBlock for it. The TextBlock does not wrap, however, so if you have too much text, it just looks silly. But knowing that the ToolTipService.ToolTip defines the Content of the ToolTip, you can add your own UIElements, and not rely on the ContentPresenter to do it for you.

Here's some XAML that makes a ToolTip that can wrap text. It also modifies some of the font properties:

<TextBox Height="20" Width="100">
    <ToolTipService.ToolTip>
        <TextBlock MaxWidth="150" 
                   Text="This is a longer string of text. It is even in a different font. Aren't ToolTips exciting?" 
                   FontFamily="Georgia" FontSize="14" TextWrapping="Wrap"/>
    </ToolTipService.ToolTip>
</TextBox>

This will show a ToolTip that is a little more interesting.

image

Because you can set the Content property to anything that you want, you can do this:

<TextBox Height="20" Width="100">
    <ToolTipService.ToolTip>
        <StackPanel>
            <Border Background="CadetBlue">
                <TextBlock Text="Some sort of title" TextAlignment="Center"/>
            </Border>
            <TextBlock MaxWidth="150" 
               Text="This is a longer string of text. It is even in a different font. Aren't ToolTips exciting?" 
               FontFamily="Georgia" FontSize="14" TextWrapping="Wrap"/>
        </StackPanel>
    </ToolTipService.ToolTip>
</TextBox>
This looks like this:

image

Note that the Content of the ToolTip can't be interacted with, so you can't put a Button (for example) in the ToolTip and expect the user to be able to interact with it, or for it to get the focus.

But this is still kind of a boring ToolTip: a plain-looking rectangle. But it is possible to make that look nicer, too.

You can retemplate the ToolTip to give it new visuals. Here's an example:

image

The XAML is below. You'll note that I rearranged things a little bit: I put a ToolTip element under the ToolTipService.ToolTip element. I made the ToolTip.Content just a simple TextBlock again, and set the ToolTip's Template property to a fancy new Template which is defined in the page resources.

<UserControl x:Class="ToolTipTest.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <UserControl.Resources>
        <ControlTemplate x:Key="ToolTipTemplate">
            <Border BorderBrush="Black" BorderThickness="4" CornerRadius="8" Background="PaleGoldenrod" MaxWidth="200">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid Margin="2">
                        <Ellipse Fill="Black" Height="52" Width="52"/>
                        <Ellipse Stroke="White" StrokeThickness="4" Fill="Blue" Height="50" Width="50"/>
                        <TextBlock Text="i" FontStyle="italic" FontSize="40" FontFamily="Georgia" 
                                               VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White"/>

                    </Grid>
                    <ContentPresenter Grid.Column="1"
                                        Content="{TemplateBinding Content}"
                                        ContentTemplate="{TemplateBinding ContentTemplate}"
                                        Margin="{TemplateBinding Padding}" 
                                        VerticalAlignment="Center"/>
                </Grid>
            </Border>
        </ControlTemplate>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="Gainsboro">
        <TextBox Height="20" Width="100">
            <ToolTipService.ToolTip>
                <ToolTip Template="{StaticResource ToolTipTemplate}">
                    <ToolTip.Content>
                        <TextBlock 
                           Text="This is a longer string of text." 
                           FontFamily="Georgia" FontSize="14" TextWrapping="Wrap"/>
                    </ToolTip.Content>
                </ToolTip>
            </ToolTipService.ToolTip>
        </TextBox>
    </Grid>
</UserControl>
Published Saturday, October 18, 2008 11:06 AM by Dave Relyea

Comments

# Customizing a Silverlight Control ToolTip

Saturday, October 18, 2008 5:50 PM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# re: Customizing a ToolTip

Saturday, October 18, 2008 5:50 PM by wisemx

Very cool, thanks for sharing. ;-)

# Silverlight Cream for October 18, 2008 -- #399

Sunday, October 19, 2008 2:22 AM by Community Blogs

In this issue: Shawn Wildermuth, Laurence Moroney, Dave Relyea, Mike Taulty, Andy Beaulieu, Brad Abrams

# re: Customizing a ToolTip

Monday, October 20, 2008 2:59 AM by JamieH

How could you manipulate this custom tooltip template from code?  For example, say I want to generate tool-tips on the fly, but I want to apply a custom template (such as the yellow background with the info icon), how would you do that?

# re: Customizing a ToolTip

Monday, October 20, 2008 11:01 AM by Dave Relyea

JamieH, this does the same thing in code:

           ToolTip tt = new ToolTip();

           tt.Template = (ControlTemplate) Resources["ToolTipTemplate"];

           tt.Content = new TextBlock() {

               FontFamily = new FontFamily("Georgia"),

               FontSize = 14, Text = "This is a longer string of text.",

               TextWrapping = TextWrapping.Wrap };

           ToolTipService.SetToolTip(textBox, tt);

# re: Customizing a ToolTip

Monday, January 19, 2009 12:01 PM by zac@wetinkpro.com

I'm getting the following two errors when I copy the last tool tip example XAML into Blend.

What am I missing?

ERROR 1: The member "Content" is not recognized or is not accessible.

ERROR 2: The member "ContentTemplate" is not recognized or is not accessible.

I'm sort of a noob and more of a designer rather than a developer so I just opened a new solution in Blend and pasted in the XAML.  Let me know what I'm missing.  THANKS!!

-zac

# re: Customizing a ToolTip

Thursday, March 05, 2009 4:12 PM by rebelBodhi

To clear up those 2 errors, you need to set the TargetType for the ControlTemplate, i.e.:

<ControlTemplate x:Key="ToolTipTemplate" TargetType="ToolTip">

(Got those errors too, thanks to <a href='http://www.emadibrahim.com/2008/03/14/silverlight-templatebinding-throwing-an-error/'>this post for the answer</a>

Anonymous comments are disabled
 
Page view tracker