Home
WPF
ToolTip Example
Updated Sep 29, 2022
Dot Net Perls
ToolTip. A ToolTip appears when the mouse hovers over a control. In WPF, we use the ToolTip attribute and the ToolTipOpening event to create ToolTips.
Getting started. This example markup has a Button control—add this by dragging it from the Toolbox to the window, where it nests within the Grid. Next add a ToolTip attribute.
Button
ToolTip code. With the ToolTip attribute, we can set static ToolTip strings. The ToolTip string is displayed when the user hovers over the control.
Here I added the ToolTipOpening event handler. In the Button_ToolTipOpening method, we dynamically set the content of the ToolTip.
Detail In Button_ToolTipOpening, we cast the sender object to a Button type. Then we set the ToolTip of the Button.
Tip The opening event occurs right before the ToolTip is displayed. So we change its value right when needed.
<Window x:Class="WpfApplication8.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Button Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" ToolTip="Not shown" ToolTipOpening="Button_ToolTipOpening"/> </Grid> </Window>
using System; using System.Windows; using System.Windows.Controls; namespace WpfApplication8 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_ToolTipOpening(object sender, ToolTipEventArgs e) { // ... Set ToolTip on Button before it is shown. Button b = sender as Button; b.ToolTip = DateTime.Now.ToShortTimeString(); } } }
A discussion. Usually a ToolTip is just a static property—so just set the ToolTip property in your XAML. Programs rarely need to set ToolTips dynamically.
Note A program can use a default ToolTip property, and only change it when needed in ToolTipOpening (or another event handler).
ToolTipClosing. This event occurs when the ToolTip goes away. As with the ToolTipOpening event handler, or any event handler, you can run arbitrary interface code here.
Summary. ToolTips in WPF are easy to add, change, and even manipulate in a dynamic way with C# code. There is more complexity to them than shown here, but this example covers the basics.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Sep 29, 2022 (edit).
Home
Changes
© 2007-2025 Sam Allen