Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable/Disable ToolTips for all controls in wpf app

Tags:

c#

wpf

tooltip

I am writing a WPF application which has a lot of different controls, each with its own ToolTip. Although the ToolTips are useful, some of them are quite long and get in the way.

I want to be able to create a button which will enable and disable all the tooltips on the app when it is clicked. what I've been working on seems like a really long and unnecessary way of going at is. Is there a way to accomplish what I want in a quick manner?

like image 816
Stavros Avatar asked Dec 13 '25 10:12

Stavros


2 Answers

You could try to add an implicit style that sets the Visbility property of all ToolTips to Collapsed for all top-level windows. Something like this:

private bool _isToolTipVisible = true;
private void Button_Click(object sender, RoutedEventArgs e)
{
    Style style = new Style(typeof(ToolTip));
    style.Setters.Add(new Setter(UIElement.VisibilityProperty, Visibility.Collapsed));
    style.Seal();

    foreach (Window window in Application.Current.Windows)
    {
        if (_isToolTipVisible)
        {
            window.Resources.Add(typeof(ToolTip), style); //hide
            _isToolTipVisible = false;
        }
        else
        {
            window.Resources.Remove(typeof(ToolTip)); //show
            _isToolTipVisible = true;
        }
    }
}
like image 136
mm8 Avatar answered Dec 15 '25 22:12

mm8


You can add global tooltip style into application resources:

<Application.Resources>
    <Style TargetType="{x:Type ToolTip}">
        <Setter Property="Template" Value="{x:Null}" />
    </Style>
</Application.Resources>

This disable all tooltips.

To toggle use following:

Style _style;

void Button_Click(object sender, RoutedEventArgs e)
{
    if (_style == null)
    {
        _style = (Style)Application.Current.Resources[typeof(ToolTip)];
        Application.Current.Resources.Remove(typeof(ToolTip));
    }
    else
        Application.Current.Resources.Add(typeof(ToolTip), _style);

}
like image 32
Sinatr Avatar answered Dec 15 '25 22:12

Sinatr