如何确保只有在按钮被禁用时才能看到按钮工具提示?
我可以将工具提示可见性绑定到什么?
答案 0 :(得分:35)
您需要在按钮上将ToolTipService.ShowOnDisabled设置为True,以便在禁用按钮时可以看到工具提示。您可以在按钮上绑定ToolTipService.IsEnabled以启用和禁用工具提示。
答案 1 :(得分:27)
这是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."/>
答案 2 :(得分:9)
您也可以使用简单的触发器来完成。只需将以下代码放入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>
答案 3 :(得分:4)
对David Ward提出的建议略有修改。这是完整的代码
添加一个值转换器,如此
<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();
}
}