none
How do you disable tooltips in code at runtime

    Question

  • Hi

    Can anyone tell me how you disable tooltips in code at runtime. I want to add an option to hide tooltips dynamically.

    regards

    Steve
    Tuesday, January 22, 2008 11:27 AM

Answers

  • If you want to disable the tool tip for the elements residing in the Window, you could try using inheritable attached property trick as illustrated at the following sample code:

    Code Snippet
    <Window x:Class="AnswerHarness.ToggleToolTipsDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cc="clr-namespace:AnswerHarness"
    Title="ToggleToolTipsDemo" Height="300" Width="300" Name="window">
    <
    StackPanel>
    <
    CheckBox IsChecked="{Binding Path=(cc:ToolTipBehavior.IsToolTipEnabled), ElementName=window}" Content="Enable ToolTip"/>
    <
    Border BorderBrush="Green" BorderThickness="1" Background="Yellow" ToolTip="Border">
    <
    StackPanel>
    <
    Button Width="120" Height="30" Content="Button1" ToolTip="Button1"/>
    <
    Button Width="120" Height="30" Content="Button2" ToolTip="Button2"/>
    <
    Button Width="120" Height="30" Content="Button3" ToolTip="Button3"/>
    </
    StackPanel>
    </
    Border>
    </
    StackPanel>
    </
    Window>
    public class ToolTipBehavior
    {
    public static Boolean GetIsToolTipEnabled(FrameworkElement obj)
    {
    return (Boolean)obj.GetValue(ToolTipEnabledProperty);
    }

    public static void SetToolTipEnabled(FrameworkElement obj, Boolean value)
    {
    obj.SetValue(ToolTipEnabledProperty, value);
    }

    public static readonly DependencyProperty ToolTipEnabledProperty = DependencyProperty.RegisterAttached(
    "IsToolTipEnabled",
    typeof(Boolean),
    typeof(ToolTipBehavior),
    new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits, (sender, e) =>
    {
    FrameworkElement element = sender as FrameworkElement;
    if (element != null)
    {
    element.SetValue(ToolTipService.IsEnabledProperty, e.NewValue);
    }
    }));
    }

    public partial class ToggleToolTipsDemo : Window
    {
    public ToggleToolTipsDemo()
    {
    InitializeComponent();

    // You can programmatically disable tool tip here.
    this.SetValue(ToolTipBehavior.ToolTipEnabledProperty, false);
    }
    }

    Hope this helps
    Thursday, January 24, 2008 4:41 AM

All replies

  • Code Block

    <Button Content="ClickMe" ToolTipService.IsEnabled ="{Binding yourBindingHere}" ToolTip="ToolTipText" />


    BR,
    ITD
    Tuesday, January 22, 2008 12:03 PM
  • Hi

    Thanks for the reply. That is certainly an option, however I'm wondering if there is a way to do it in code iteratively before I go and change all of my xaml.

    regards

    Steve
    Tuesday, January 22, 2008 12:05 PM
  • If you want to disable the tool tip for the elements residing in the Window, you could try using inheritable attached property trick as illustrated at the following sample code:

    Code Snippet
    <Window x:Class="AnswerHarness.ToggleToolTipsDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cc="clr-namespace:AnswerHarness"
    Title="ToggleToolTipsDemo" Height="300" Width="300" Name="window">
    <
    StackPanel>
    <
    CheckBox IsChecked="{Binding Path=(cc:ToolTipBehavior.IsToolTipEnabled), ElementName=window}" Content="Enable ToolTip"/>
    <
    Border BorderBrush="Green" BorderThickness="1" Background="Yellow" ToolTip="Border">
    <
    StackPanel>
    <
    Button Width="120" Height="30" Content="Button1" ToolTip="Button1"/>
    <
    Button Width="120" Height="30" Content="Button2" ToolTip="Button2"/>
    <
    Button Width="120" Height="30" Content="Button3" ToolTip="Button3"/>
    </
    StackPanel>
    </
    Border>
    </
    StackPanel>
    </
    Window>
    public class ToolTipBehavior
    {
    public static Boolean GetIsToolTipEnabled(FrameworkElement obj)
    {
    return (Boolean)obj.GetValue(ToolTipEnabledProperty);
    }

    public static void SetToolTipEnabled(FrameworkElement obj, Boolean value)
    {
    obj.SetValue(ToolTipEnabledProperty, value);
    }

    public static readonly DependencyProperty ToolTipEnabledProperty = DependencyProperty.RegisterAttached(
    "IsToolTipEnabled",
    typeof(Boolean),
    typeof(ToolTipBehavior),
    new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits, (sender, e) =>
    {
    FrameworkElement element = sender as FrameworkElement;
    if (element != null)
    {
    element.SetValue(ToolTipService.IsEnabledProperty, e.NewValue);
    }
    }));
    }

    public partial class ToggleToolTipsDemo : Window
    {
    public ToggleToolTipsDemo()
    {
    InitializeComponent();

    // You can programmatically disable tool tip here.
    this.SetValue(ToolTipBehavior.ToolTipEnabledProperty, false);
    }
    }

    Hope this helps
    Thursday, January 24, 2008 4:41 AM
  • Wow, that looks pretty nifty. I'll have to give that a go. At the moment I've done it iteratively but it's not ideal because of having to handle different types.

    Code Snippet

    public static void SetToolTipEnabled(Object obj, bool enabled)
            {
               

                if (obj is FrameworkElement)
                {
                    FrameworkElement fe = (FrameworkElement)obj;

                    if (obj is System.Windows.Controls.Control)
                    {
                        System.Windows.Controls.ToolTipService.SetIsEnabled(obj as System.Windows.Controls.Control, enabled);
                    }
                    else if (obj is System.Windows.Controls.Image)
                    {
                        System.Windows.Controls.ToolTipService.SetIsEnabled(obj as System.Windows.Controls.Image, enabled);
                    }

                    // recurse through the children
                    IEnumerable children = LogicalTreeHelper.GetChildren(fe);
                    foreach (object child in children)
                    {
                        SetToolTipEnabled(child, enabled);
                    }

                }


            }



    Thursday, January 24, 2008 5:03 AM
  • Thanks a lot. It's very much helpful for me.
    Friday, April 08, 2011 10:04 AM