Search code examples
c#wpftooltip

Enable/Disable ToolTips for all controls in wpf app


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?


Solution

  • 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;
            }
        }
    }