WPF 工具提示可见性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3149016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Tooltip Visibility
提问by David Ward
How can I ensure that a button's Tooltip is only visible when the button is disabled?
如何确保按钮的工具提示仅在按钮禁用时可见?
What can I bind the tooltip's visibility to?
我可以将工具提示的可见性绑定到什么?
回答by Quartermeister
You will need to set ToolTipService.ShowOnDisabledto True on the Button in order to have the Tooltip visible at all when the Button is disabled. You can bind ToolTipService.IsEnabledon the Button to enable and disable the Tooltip.
您需要在 Button上将 ToolTipService.ShowOnDisabled设置为 True,以便在禁用 Button 时让 Tooltip 完全可见。您可以在 Button 上绑定ToolTipService.IsEnabled来启用和禁用 Tooltip。
回答by David Ward
This is the full XAML of the Button (based on the answer of @Quartermeister)
这是按钮的完整 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."/>
回答by Daniel
You can do it using a simple trigger also. Just place the following piece of code into a Window.
您也可以使用简单的触发器来完成。只需将以下代码段放入 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>
回答by Sriwantha Attanayake
A slightly modified answer for what David Ward has proposed. Here is the full code
对大卫·沃德 (David Ward) 提出的建议稍加修改的答案。这是完整的代码
Add a value converter to resouces like this
为这样的资源添加一个值转换器
<Window.Resources>
<Converters:NegateConverter x:Key="negateConverter"/>
</Window.Resources>
Then define following xaml
然后定义以下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"/>
The value converter looks like this
值转换器看起来像这样
[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();
}
}