BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Base Classes - FrameworkElement - Tool Tips

The twenty-seventh part of the Windows Presentation Foundation Fundamentals tutorial continues to describe the FrameworkElement type. This instalment describes some of the properties that control the display of tool tips.

Tool Tip Status Properties

The final three properties that we'll consider in this article relate to the status of a tool tip. The first is IsOpen. This read-only property can be used to determine whether a tool tip is visible or not. The property value changes to true when the tool tip is displayed and returns to false when it is hidden.

You can use IsEnabled to enable or disable your tool tips. If you set the property to false, the tool tip will not be displayed when the user hovers over its control.

With default settings, tool tips that are defined for disabled controls will not appear. You can override this behaviour so that tool tips are shown for disabled controls by setting ShowOnDisabled to true. However, if the tool tip itself is disabled with the IsEnabled property, it will always remain hidden.

Tool Tip Events

In the final section we'll look at two events relating to tool tips, both of which are provided by the FrameworkElement class. In some situations you might wish your code to react to a tool tip being displayed or disappearing from the screen. For example, you might have a complex, animated tool tip and wish to reset the animation when the tool tip is shown or hidden. You can detect these actions with the ToolTipOpening and ToolTipClosing events.

ToolTipOpening is raised when the tool tip is displayed, following the delay determined by the InitialShowDelay property. ToolTipClosing fires when the tool tip disappears, either after the ShowDuration period expires or when the user moves the mouse pointer outside of the control to which the tool tip is connected.

To demonstrate, first modify the Cancel button's XAML to register the two events, as follows:

<Button Content="Cancel"
        Width="85"
        VerticalAlignment="Center"
        ToolTip="Cancel the login and exit"
        ToolTipService.HorizontalOffset="10"
        ToolTipService.VerticalOffset="-0.5in"
        ToolTipService.HasDropShadow="False"
        ToolTipService.InitialShowDelay="3000"
        ToolTipService.ShowDuration="1000"
        ToolTipOpening="Button_ToolTipOpening"
        ToolTipClosing="Button_ToolTipClosing"/>

We can now add some functionality to the C# code behind the window. The two methods shown below are not particularly useful but make the action of the event easy to see. When the tool tip for the Cancel button appears, the background of the window is painted red. When the tip disappears, the window colour returns to the original setting.

private void Button_ToolTipOpening(object sender, ToolTipEventArgs e)
{
    Background = Brushes.Red;
}

private void Button_ToolTipClosing(object sender, ToolTipEventArgs e)
{
    Background = SystemColors.WindowBrush;
}
18 September 2013