Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Programmatically show a WPF/C# Windows.Control.ToolTip?

There doesn't seem to be a .Show() kind of method for the Windows.Control.ToolTip, incl in ToolTipService.

like image 687
MrGreggles Avatar asked Oct 01 '09 09:10

MrGreggles


People also ask

How do I display a WPF window?

When a Window is created at run-time using the Window object, it is not visible by default. To make it visible, we can use Show or ShowDialog method. Show method of Window class is responsible for displaying a window.

How can I find WPF controls by name or type?

FindName method of FrameworkElement class is used to find elements or controls by their Name properties. This article shows how to find controls on a Window by name. FindName method of FrameworkElement class is used to find elements or controls by their Name properties.


2 Answers

What you need to do is make sure the ToolTip on the control is of type ToolTip. Then you can set the IsOpen property to true like so:

ToolTip tooltip = new ToolTip{ Content = "My Tooltip" }; NameTextBox.ToolTip = tooltip; tooltip.IsOpen = true;     
like image 143
Ray Avatar answered Sep 22 '22 17:09

Ray


If you would like to control how long the tooltip remains open, you can subscribe to the Opened event and set a time delay before closing the tooltip.

Subscription has to be done before IsOpen = true and it has to be an async method to avoid hanging up the UI.

var tooltip = new ToolTip { Content = "New tooltip text" }; MyControln.ToolTip = tooltip; tooltip.Opened += async delegate (object o, RoutedEventArgs args) {     var s = o as ToolTip;     // let the tooltip display for 1 second     await Task.Delay(1000);     s.IsOpen = false;     // wait till the close tooltip animation finishes before changing to old tooltip text     await Task.Delay(1000);     s.Content = "Old tooltip text"; }; tooltip.IsOpen = true; 
like image 40
Anthony Avatar answered Sep 19 '22 17:09

Anthony