如何确保按钮禁用时仅显示按钮工具提示?
我可以将工具提示的可见性绑定到什么上?
如何确保按钮禁用时仅显示按钮工具提示?
我可以将工具提示的可见性绑定到什么上?
如果想要在按钮被禁用时仍然显示工具提示,需将ToolTipService.ShowOnDisabled设置为True。可以绑定ToolTipService.IsEnabled来启用或禁用工具提示。
这是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."/>
你也可以使用一个简单的触发器来完成。只需将以下代码片段放入一个窗口中即可。
<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>
Style
设置器查看设置ShowOnDisabled
的语法。 - mungflesh这是对David Ward提出的建议稍作修改后的答案。以下是完整代码:
向资源添加值转换器,格式如下:
<Window.Resources>
<Converters:NegateConverter x:Key="negateConverter"/>
</Window.Resources>
<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();
}
}