C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.
Cast: In Button_ToolTipOpening, we cast the sender object to a Button type. Then we set the ToolTip of the Button.
AsTip: The opening event occurs right before the ToolTip is displayed. So we change its value right when needed.
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();
}
}
}
Note: A program can use a default ToolTip property, and only change it when needed in ToolTipOpening (or another event handler).