2010-06-30 136 views
20

如何確保按鈕的工具提示僅在禁用按鈕時可見?WPF工具提示可見性

我可以將工具提示的可見性綁定到什麼地方?

+0

何時「禁用」? - 你的意思是「不是」禁用? – 4imble 2010-06-30 12:15:31

+8

顯示工具提示可能會有意義,說明您無法觸摸此按鈕的原因。如果這是戴維的意圖,我認爲這很有道理。 – reuscam 2010-06-30 12:17:57

+1

是的,我想是的,我沒有挑剔。我只是真正感興趣:) – 4imble 2010-06-30 12:20:38

回答

28

您需要在按鈕上將ToolTipService.ShowOnDisabled設置爲True才能在禁用Button時使Tooltip可見。您可以在按鈕上綁定ToolTipService.IsEnabled以啓用和禁用工具提示。

+1

對於任何想和我做同樣事情的人,我已經發布了按鈕的完整xaml作爲答案。 感謝您的幫助。 – 2010-06-30 13:54:41

20

這是按鈕全XAML(基於@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

稍微修改回答什麼大衛沃德建議。下面是完整的代碼

添加值轉換器resouces這樣

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

然後定義下面的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"/> 

值變換器看起來像這樣

[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

你可以也使用簡單的觸發器來做到這一點。只需將下面的一段代碼放入一個窗口即可。

<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

通過'Style'設置器,可以方便地查看設置'ShowOnDisabled'的語法。 – mungflesh 2016-08-12 10:55:43