wpf: how to show tooltip when button disabled by command?
WpfWpf Problem Overview
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.
Wpf Solutions
Solution 1 - Wpf
ToolTipService.ShowOnDisabled="True"
Solution 2 - Wpf
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.
Solution 3 - Wpf
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.