TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

WPF ToolTip Example

This WPF example uses the ToolTip attribute and the ToolTipOpening event. It has an example screenshot.

ToolTip. A ToolTip appears when the mouse hovers over a control.

It is meant to provide information about the control. In WPF, we use the ToolTip attribute and the ToolTipOpening event to create ToolTips.

Based on:

.NET 4.5.1

Example. This example markup has a Button control. You can add this by dragging it from the Toolbox to the window, where it nests within the Grid. I added a ToolTip attribute. With this attribute, we can set static ToolTip strings.

Button

And: The ToolTip string is displayed when the user hovers over the control. Many controls, not just buttons, can have ToolTips.

Also, I added the ToolTipOpening event handler. In the Button_ToolTipOpening method, we dynamically set the content of the ToolTip. This example shows a dynamically-set ToolTip value.

Example markup: XAML

<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>

Example code: C#

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();
	}
    }
}

In Button_ToolTipOpening, we cast the sender object to a Button type. Then we set the ToolTip of the Button. The opening event occurs right before the ToolTip is displayed. So we change its value right when needed.

Tip: We use the DateTime.Now property, and the ToShortTimeString method, to get the current time.

DateTime.NowDateTime Format

Discussion. The example does not show this, but usually a ToolTip is just a static property. So just set the ToolTip property in your XAML. Programs less often 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).

Also, a ToolTipClosing event is available. This occurs (obviously) when the ToolTip goes away. As with the ToolTipOpening event handler, or any event handler, you can run arbitrary interface code here.

 

Summary. They are an essential part of many interfaces. And 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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf