Qua*_*ter 37
您需要在Button 上将ToolTipService.ShowOnDisabled设置为True,以便在禁用Button时让Tooltip可见.您可以在Button上绑定ToolTipService.IsEnabled以启用和禁用Tooltip.
Dav*_*ard 31
这是Button的完整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."/>
Dan*_*iel 11
您也可以使用简单的触发器来完成.只需将以下代码放入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>
对大卫·沃德提出的答案略有修改。这是完整的代码
像这样向资源添加一个值转换器
<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();
    }
  }
| 归档时间: | 
 | 
| 查看次数: | 22890 次 | 
| 最近记录: |