204

I'm trying to show a tooltip regardless of a buttons state, but this does not seem to do the trick:

<Button Command="{Binding Path=CommandExecuteAction}" 
        ToolTip="{Binding Path=Description}" ToolTipService.ShowOnDisabled="true"
        Style="{StaticResource toolbarButton}">
   <Image Source="{Binding Path=Icon}"></Image>
</Button>

How can i show the tooltip when the button is disabled due to command.CanExecute returning false?

Note:

ToolTipService.ShowOnDisabled="true" works like a charm. The reason this didn't work in my example is because the style associated with the button redefines the controltemplate and turned off hit-testing on the button when the button was disabled (IsHitTestVisible=false). Re-enabling hit-testing in the controltemplate made the tooltip appear when the button was disabled.

Indy9000
  • 8,651
  • 2
  • 32
  • 37
Marius
  • 9,208
  • 8
  • 50
  • 73
  • possible duplicate of [WPF Tooltip Visibility](http://stackoverflow.com/questions/3149016/wpf-tooltip-visibility) – OJ. Nov 11 '10 at 10:45
  • I'm using the ToolTipService.ShowOnDisabled, but its not working. – Marius Nov 11 '10 at 11:10
  • 1
    Just delete this question. I did a small test project and ToolTipService.ShowOnDisabled works just fine. – Marius Nov 11 '10 at 11:33
  • 23
    I'm glad this question didn't get deleted. It quickly and accurately answered a question/problem I had, which is the exact reason I come to SO in the first place. Thanks for being Lazy(tm) Marius. :-) – Jere.Jones Dec 18 '10 at 18:21
  • Excuse me, is there a way I can show it only when disabled? – advapi Nov 13 '19 at 14:07
  • @advapi Try assigning the Tooltip text only when control is disabled, otherwise set empty Tooltip. – Sen Jacob Nov 19 '19 at 10:02

3 Answers3

367

You can use on xaml element directly:

<Grid ToolTipService.ShowOnDisabled="True" ... >
Gh61
  • 9,222
  • 4
  • 28
  • 39
Kishore Kumar
  • 21,449
  • 13
  • 81
  • 113
  • 11
    MSDN reference: http://msdn.microsoft.com/en-us/library/system.windows.controls.tooltipservice.aspx – David Mar 08 '13 at 05:27
  • 11
    This goes in the xaml declaration for the object on which the tooltip will appear, i.e.: ` – gusmally supports Monica Nov 04 '19 at 18:44
  • 1
    If someone finds this now, Visual Studios updated this API. It is now: ToolTipService.SetShowOnDisabled. The first parameter takes in the DependencyObject (i.e, your button and such) and the second parameter is a bool. – user2529011 Feb 03 '21 at 20:15
  • @user2529011 Visual Studio? Or are we talking .NET (Core)? I don't get how a visual studio API is going to effect anything aside from VS extensions or plugins... – Narish Dec 15 '22 at 15:52
41

This is a good method to add to your startup code:

ToolTipService.ShowOnDisabledProperty.OverrideMetadata(
    typeof(FrameworkElement),
    new FrameworkPropertyMetadata(true));

It ensures that for any class inheriting from FrameworkElement, tooltips are shown even if a control instance is disabled. This covers all elements that can have a tooltip.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
sacha barber
  • 2,214
  • 1
  • 24
  • 37
  • Can you explain what this does? Saying it's a good method at startup doesn't explain. – Stealth Rabbi Jan 22 '19 at 12:35
  • It ensures that for any class inheriting from Control, tooltips are shown even if Control instance is disabled – sacha barber Jan 22 '19 at 18:24
  • 12
    It is ironic that users have the greatest need for the tooltip *when* the control is disabled, as they want to know *why* the control is disabled. This is a good example of a default in WPF that is incorrect the vast majority of the time. So it's worth changing it. – Contango Sep 04 '19 at 07:16
  • 1
    Edited to use *typeof (FrameworkElement)* rather than *typeof(Control)* as this covers all elements that can have a tooltip. – xtempore Jan 11 '21 at 20:59
7

Make tooltip visible for ALL disabled Buttons and Checkboxes:

<Window.Resources>
    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}>
        <Setter Property="ToolTipService.ShowOnDisabled" Value="true"/>
    </Style>
    <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource {x:Type CheckBox}}>
        <Setter Property="ToolTipService.ShowOnDisabled" Value="true"/>
    </Style>
</Window.Resources>

The BasedOn=... prevents that you lose any other styles that have been applied to checkbox or button before. If you don't use any other styles for button or checkbox you can remove the BasedOn=.. parts.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Welcor
  • 2,431
  • 21
  • 32