| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | |||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ToolTip Overview |
03/30/2017 |
.net-framework |
|
article |
|
f06c1603-e9cb-4809-8a62-234607fc52f7 |
22 |
dotnet-bot |
dotnetcontent |
wpickett |
ToolTip Overview
A tooltip is a small pop-up window that appears when a user pauses the mouse pointer over an element, such as over a xref:System.Windows.Controls.Button. This topic introduces the tooltip and discusses how to create and customize tooltip content.
What Is a Tooltip?
When a user moves the mouse pointer over an element that has a tooltip, a window that contains tooltip content (for example, text content that describes the function of a control) appears for a specified amount of time. If the user moves the mouse pointer away from the control, the window disappears because the tooltip content cannot receive focus.
The content of a tooltip can contain one or more lines of text, images, shapes, or other visual content. You define a tooltip for a control by setting one of the following properties to the tooltip content.
-
xref:System.Windows.FrameworkContentElement.ToolTip%2A?displayProperty=nameWithType
-
xref:System.Windows.FrameworkElement.ToolTip%2A?displayProperty=nameWithType
Which property you use depends on whether the control that defines the tooltip inherits from the xref:System.Windows.FrameworkContentElement or xref:System.Windows.FrameworkElement class.
Creating a ToolTip
The following example shows how to create a simple tooltip by setting the xref:System.Windows.FrameworkElement.ToolTip%2A property for a xref:System.Windows.Controls.Button control to a text string.
[!code-xamlGroupBoxSnippet#ToolTipString]
You can also define a tooltip as a xref:System.Windows.Controls.ToolTip object. The following example uses [!INCLUDETLA2#tla_xaml] to specify a xref:System.Windows.Controls.ToolTip object as the tooltip of a xref:System.Windows.Controls.TextBox element. Note that the example specifies the xref:System.Windows.Controls.ToolTip by setting the xref:System.Windows.FrameworkElement.ToolTip%2A?displayProperty=nameWithType property.
[!code-xamlToolTipSimple#ToolTip]
The following example uses code to generate a xref:System.Windows.Controls.ToolTip object. The example creates a xref:System.Windows.Controls.ToolTip (tt) and associates it with a xref:System.Windows.Controls.Button.
[!code-csharpToolTipSimple#2] [!code-vbToolTipSimple#2]
You can also create tooltip content that is not defined as a xref:System.Windows.Controls.ToolTip object by enclosing the tooltip content in a layout element, such as a xref:System.Windows.Controls.DockPanel. The following example shows how to set the xref:System.Windows.FrameworkElement.ToolTip%2A property of a xref:System.Windows.Controls.TextBox to content that is enclosed in a xref:System.Windows.Controls.DockPanel control.
[!code-xamlGroupBoxSnippet#ToolTipDockPanel]
Using the Properties of the ToolTip and ToolTipService Classes
You can customize tooltip content by setting visual properties and applying styles. If you define the tooltip content as a xref:System.Windows.Controls.ToolTip object, you can set the visual properties of the xref:System.Windows.Controls.ToolTip object. Otherwise, you must set equivalent attached properties on the xref:System.Windows.Controls.ToolTipService class.
For an example of how to set properties in order to specify the position of tooltip content by using the xref:System.Windows.Controls.ToolTip and xref:System.Windows.Controls.ToolTipService properties, see Position a ToolTip.
Styling a ToolTip
You can style a xref:System.Windows.Controls.ToolTip by defining a custom xref:System.Windows.Style. The following example defines a xref:System.Windows.Style called Simple that shows how to offset the placement of the xref:System.Windows.Controls.ToolTip and change its appearance by setting the xref:System.Windows.Controls.Control.Background%2A, xref:System.Windows.Controls.Control.Foreground%2A, xref:System.Windows.Controls.Control.FontSize%2A, and xref:System.Windows.Controls.Control.FontWeight%2A.
[!code-xamlToolTipSimple#Style]
Using the Time Interval Properties of ToolTipService
The xref:System.Windows.Controls.ToolTipService class provides the following properties for you to set tooltip display times: xref:System.Windows.Controls.ToolTipService.InitialShowDelay%2A, xref:System.Windows.Controls.ToolTipService.BetweenShowDelay%2A, and xref:System.Windows.Controls.ToolTipService.ShowDuration%2A.
Use the xref:System.Windows.Controls.ToolTipService.InitialShowDelay%2A and xref:System.Windows.Controls.ToolTipService.ShowDuration%2A properties to specify a delay, typically brief, before a xref:System.Windows.Controls.ToolTip appears and also to specify how long a xref:System.Windows.Controls.ToolTip remains visible. For more information, see How to: Delay the Display of a ToolTip.
The xref:System.Windows.Controls.ToolTipService.BetweenShowDelay%2A property determines if tooltips for different controls appear without an initial delay when you move the mouse pointer quickly between them. For more information about the xref:System.Windows.Controls.ToolTipService.BetweenShowDelay%2A property, see Use the BetweenShowDelay Property.
The following example shows how to set these properties for a tooltip.
[!code-xamlToolTipService#ToolTip]
See Also
xref:System.Windows.Controls.ToolTipService
xref:System.Windows.Controls.ToolTip
xref:System.Windows.Controls.ToolTipEventArgs
xref:System.Windows.Controls.ToolTipEventHandler
How-to Topics