2010-05-23 16 views
16

Basta chiedersi se è possibile mostrare un WPF su un articolo disattivato SOLO (e non quando l'elemento è abilitato).Mostra solo suggerimento WPF su articolo disattivato

Vorrei fornire all'utente una descrizione del motivo per cui un articolo è attualmente disabilitato.

Ho un IValueConverter per invertire l'associazione booleana IsEnabled. Ma non sembra funzionare in questa situazione. Lo ToolTip viene visualizzato sia quando l'elemento è abilitato e disabilitato.

Quindi è possibile associare una proprietà ToolTip.IsEnabled esclusivamente a un oggetto proprio! ?

domanda abbastanza semplice credo, ma codice di esempio qui comunque:

public class BoolToOppositeBoolConverter : IValueConverter 
{ 
    #region IValueConverter Members 

    public object Convert(object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     if (targetType != typeof(bool)) 
      throw new InvalidOperationException("The target must be a boolean"); 

     return !(bool)value; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     if (targetType != typeof(bool)) 
      throw new InvalidOperationException("The target must be a boolean"); 

     return !(bool)value; 
    } 

    #endregion 
} 

E il legame:

<TabItem Header="Tab 2" Name="tabItem2" ToolTip="Not enabled in this situation." ToolTipService.ShowOnDisabled="True" ToolTipService.IsEnabled="{Binding Path=IsEnabled, ElementName=tabItem2, Converter={StaticResource oppositeConverter}}"> 
    <Label Content="Item content goes here" /> 
</TabItem> 

Grazie gente.

+0

Sei sicuro che ToolTipService.ShowOnDisabled = "True" non sta eseguendo "dopo" la tua inversione? Sembra che solo il binding abilitato debba essere necessario. – JustABill

+0

@JUSTABill: Questo potrebbe essere il caso, ma non funziona senza ToolTipService.ShowOnDisabled = "True". Forse ho bisogno di gestirlo in code-behind. Preferirei conservare le informazioni della GUI in XAML, se possibile. – dant

+0

In questo caso, ti suggerisco di associare Tooltip come ToolTip = "{Binding ElementName = tabItem2, Path = IsEnabled, Converter = {StaticResource newconverter}, ConverterParameter = Il testo del tooltip effettivo va qui}", dove newconverter è un nuovo tipo che restituisce il valore nel parametro se il valore è true. O falso nel tuo caso, credo. (Anche io l'ho digitato dalla memoria quindi perdonami se la sintassi è disattivata) – JustABill

risposta

20

Il suggerimento di JustABill ha funzionato. Avevo anche bisogno di definire la stringa come una risorsa per evitare problemi con le virgolette. E hai ancora bisogno di impostare ToolTipService.ShowOnDisabled = "True".

Quindi, ecco il codice di lavoro che mostra come visualizzare un tooltip in WPF solo quando un elemento è disattivata.

Nel contenitore superiore, includere lo spazio dei nomi di sistema (vedere sys di seguito). Ho anche uno spazio dei nomi Risorse, che ho chiamato "Res".

<Window x:Class="MyProjectName.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:sys="clr-namespace:System;assembly=mscorlib" 
    xmlns:Res="clr-namespace:MyProjectName.Resources" 
    > 

allora avete bisogno

<Window.Resources> 
    <Res:FalseToStringConverter x:Key="falseToStringConv" /> 
    <sys:String x:Key="stringToShowInTooltip">This item is disabled because...</sys:String> 
</Window.Resources> 

Nel mio caso, è stato un elemento di scheda che mi interessava. Potrebbe essere qualsiasi elemento dell'interfaccia utente anche se ...

<TabItem Name="tabItem2" ToolTipService.ShowOnDisabled="True" ToolTip="{Binding Path=IsEnabled, ElementName=tabItem2, Converter={StaticResource falseToStringConv}, ConverterParameter={StaticResource stringToShowInTooltip}}"> 
      <Label Content="A label in the tab" /> 
</TabItem> 

E il convertitore in codice dietro (o dove vuoi metterlo). Nota, il mio è entrato in un namespace chiamato Risorse, che è stato dichiarato in precedenza.

public class FalseToStringConverter : IValueConverter 
{ 
    #region IValueConverter Members 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value is bool && parameter is string) 
     { 
      if ((bool)value == false) 
       return parameter.ToString(); 
      else return null; 
     } 
     else 
      throw new InvalidOperationException("The value must be a boolean and parameter must be a string"); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
    #endregion 
} 
+12

+1 per ToolTipService.ShowOnDisabled = "True" – Tim

5

Un po 'fuori moda, ma ho ottenuto questo lavoro impostando la modalità RelativeSource a Self invece di impostare l'ElementName all'interno della rilegatura.

<TabItem Header="Tab 2" Name="tabItem2" ToolTip="Not enabled in this situation." ToolTipService.ShowOnDisabled="True" ToolTipService.IsEnabled="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource oppositeConverter}}"> 
    <Label Content="Item content goes here" /> 
</TabItem> 
+0

risposta molto bella – stambikk

Problemi correlati