2010-06-30 12 views
20

¿Cómo puedo asegurarme de que la información sobre herramientas de un botón solo está visible cuando el botón está desactivado?WPF Tooltip Visibilidad

¿A qué puedo vincular la visibilidad de la información sobre herramientas?

+0

Cuando "IS" está deshabilitado? - ¿Querías decir "no está" deshabilitado? – 4imble

+8

Podría tener sentido mostrar una información sobre herramientas que describa por qué no puede tocar este botón. Si ese es el intento de David, creo que tiene mucho sentido. – reuscam

+1

Sí, supongo que sí, no estaba siendo exigente. Estaba genuinamente interesado :) – 4imble

Respuesta

28

Deberá establecer ToolTipService.ShowOnDisabled en True on the Button para que la información sobre herramientas permanezca visible cuando el botón esté deshabilitado. Puede enlazar ToolTipService.IsEnabled en el botón para activar y desactivar la información sobre herramientas.

+1

Para cualquiera que quiera hacer las mismas cosas que yo, he publicado el xaml completo para el botón como respuesta. Gracias por su ayuda. –

20

Este es el XAML total del botón (basado en la respuesta de @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."/> 
3

Una respuesta ligeramente modificada para lo que se ha propuesto David Ward. Aquí está el código completo

Añadir un convertidor de valores a los resouces como esto

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

A continuación, defina siguiente 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"/> 

El convertidor de valores se ve así

[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(); 
    } 
    } 
8

Puede hazlo usando un simple disparador también. Simplemente coloque la siguiente pieza de código en una ventana.

<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> 
+0

Esto fue útil para ver la sintaxis para configurar el 'ShowOnDisabled' a través de un setter' Style'. – mungflesh

Cuestiones relacionadas