31

How can I ensure that a button's Tooltip is only visible when the button is disabled?

What can I bind the tooltip's visibility to?

David Ward
  • 3,739
  • 10
  • 44
  • 66

4 Answers4

46

You will need to set ToolTipService.ShowOnDisabled to True on the Button in order to have the Tooltip visible at all when the Button is disabled. You can bind ToolTipService.IsEnabled on the Button to enable and disable the Tooltip.

Quartermeister
  • 57,579
  • 7
  • 124
  • 111
  • 2
    For anyone who wants to do the same things as me, I've posted the full xaml for the button as an answer. Thanks for your help. – David Ward Jun 30 '10 at 13:54
37

This is the full XAML of the Button (based on the answer of @Quartermeister)

<Button 
  x:Name="btnAdd" 
  Content="Add" 
  ToolTipService.ShowOnDisabled="True" 
  ToolTipService.IsEnabled="{Binding ElementName=btnAdd, Path=IsEnabled, Converter={StaticResource boolToOppositeBoolConverter}}" 
  ToolTip="Appointments cannot be added whilst the event has outstanding changes."/>
Breeze
  • 2,010
  • 2
  • 32
  • 43
David Ward
  • 3,739
  • 10
  • 44
  • 66
12

You can do it using a simple trigger also. Just place the following piece of code into a Window.

<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
    <CheckBox Name="chkDisabler" Content="Enable / disable button" Margin="10" />
    <Button Content="Hit me" Width="200" Height="100" IsEnabled="{Binding ElementName=chkDisabler, Path=IsChecked}">
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="ToolTipService.ShowOnDisabled" Value="true" />
                <Setter Property="ToolTip" Value="{x:Null}" />
                <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="ToolTip" Value="Hi, there! I'm disabled!" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>
</StackPanel>
Daniel
  • 444
  • 5
  • 7
  • This was handy to see the syntax for setting the `ShowOnDisabled` via a `Style` setter. – mungflesh Aug 12 '16 at 10:55
  • If I interpret this code correctly, one can set two triggers to deliver two different tooltips, one for button disabled and one for enabled? – Mike Sep 16 '18 at 14:02
  • Yes Mike, You could add a second trigger for IsEnabled and True for that. – Josh Aug 26 '19 at 16:20
5

A slightly modified answer for what David Ward has proposed. Here is the full code

Add a value converter to resouces like this

<Window.Resources>
    <Converters:NegateConverter x:Key="negateConverter"/>
</Window.Resources>

Then define following xaml

<Button 
  x:Name="btnAdd" 
  Content="Add" 
  ToolTipService.ShowOnDisabled="True" 
  ToolTipService.IsEnabled="{Binding RelativeSource={RelativeSource self}, Path=IsEnabled, Converter={StaticResource negateConverter}}" 
  ToolTip="Hi guys this is the tool tip"/>

The value converter looks like this

[ValueConversion(typeof(bool), typeof(bool))]
  public class NegateConverter : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
     return  !((bool)value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      throw new NotImplementedException();
    }
  }
Sriwantha Attanayake
  • 7,694
  • 5
  • 42
  • 44