locked
How to enable and disable all tooltips in code RRS feed

  • Question

  • I am writing a WPF application that contains lots of tooltips. This is handy for new users, but becomes tiresome for familiar users.

    Is there a simple way to enable/disable all tooltips in one go (in code)?

    Ideally, I'd hope to use a checkable menu item which can turn on and turn off tooltips for all child elements.

    I've tried this recursive solution (see cheesetarmac's post at the bottom)
    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/98471070-06ee-481e-b586-08b9fb4f15eb
    But this line is causing errors:
    IEnumerable children = LogicalTreeHelper.GetChildren(fe);
    
    The error is:
    Using the generic type 'System.Collections.Generic.IEnumerable<T>' requires '1' type arguments


    Thanks in advance.
    Tuesday, January 12, 2010 7:46 AM

Answers

  • Hi DTAH,
     
    I think the recursive solution in cheesetarmac's post is exactly what you want, and it can solve you problem perfectly. But the interface IEnumerable in you post is not the generic type System.Collections.Generic.IEnumerable<T>, actually it is System.Collections.IEnumerable.
     
    So, you use reference the IEnumerable interface with full namespace in you program it will works ok. And I have implement the simple solution for you as follows:
     
            public void SetTooltipEnabled(object obj, bool enabled)
            {
                if (obj is FrameworkElement)
                {
                    FrameworkElement fe = obj as FrameworkElement;
                    System.Windows.Controls.ToolTipService.SetIsEnabled(fe, enabled);
                    System.Collections.IEnumerable children = LogicalTreeHelper.GetChildren(fe);
                    foreach (object child in children)
                    {
                        SetTooltipEnabled(child, enabled);
                    }
                }
            }
    Hope this helps. If you have any question, please feel free let me know.
     
    Best regards,
    Kevin
    • Marked as answer by DrDanielT Sunday, January 17, 2010 10:34 PM
    Saturday, January 16, 2010 12:35 PM

All replies

  • Hi DTAH,
     
    I think the recursive solution in cheesetarmac's post is exactly what you want, and it can solve you problem perfectly. But the interface IEnumerable in you post is not the generic type System.Collections.Generic.IEnumerable<T>, actually it is System.Collections.IEnumerable.
     
    So, you use reference the IEnumerable interface with full namespace in you program it will works ok. And I have implement the simple solution for you as follows:
     
            public void SetTooltipEnabled(object obj, bool enabled)
            {
                if (obj is FrameworkElement)
                {
                    FrameworkElement fe = obj as FrameworkElement;
                    System.Windows.Controls.ToolTipService.SetIsEnabled(fe, enabled);
                    System.Collections.IEnumerable children = LogicalTreeHelper.GetChildren(fe);
                    foreach (object child in children)
                    {
                        SetTooltipEnabled(child, enabled);
                    }
                }
            }
    Hope this helps. If you have any question, please feel free let me know.
     
    Best regards,
    Kevin
    • Marked as answer by DrDanielT Sunday, January 17, 2010 10:34 PM
    Saturday, January 16, 2010 12:35 PM
  • It works, thanks.

    I've also found that if you have a chart with axis tooltips, you may also need something like the following code after the foreach loop:

    if (obj is Chart)
    {
        Chart ch = (Chart)obj;
        for (int i = 0; i < ch.Axes.Count; i++)
        {
            setAllToolTips(ch.Axes[i], enabled);
        }
    }

    Other than that, the recursion seems to drill down and grab all the tooltips.
    Sunday, January 17, 2010 10:34 PM