使工具提示不可见 [英] Making a ToolTip not visible
问题描述
有一个我希望始终可见的图标,但我希望工具提示有条件地可见.这是我目前拥有的代码:
There is an icon that I want to always be visible, but I want the tooltip to be conditionally visible. Here is the code that I currently have:
<TextBlock Grid.Row="2"
Grid.Column="0"
VerticalAlignment="Center"
FontSize="15"
Visibility="{Binding IsConnected, Converter={StaticResource BooleanToVisibilityConverter}}">
<fa:ImageAwesome Icon="{Binding Path=BatteryLevelIcon, UpdateSourceTrigger=PropertyChanged}"
Height="20"
Width="20"
Foreground="Green"
Visibility="{Binding IsConnected, Converter={StaticResource BooleanToVisibilityConverter}}" />
<ToolTipService.ToolTip>
<TextBlock Visibility="{Binding IsCharging, Converter={StaticResource InvertedBooleanToVisibilityConverter}}">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}%">
<Binding Path="BatteryPercentage" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</ToolTipService.ToolTip>
</TextBlock>
因此,我希望工具提示仅在 IsCharging 为 false 时显示.我遇到的问题是,因为 Visibility 属性位于工具提示文本块而不是工具提示本身,将其设置为不可见只会给我一个空的工具提示,而不是工具提示根本不出现.我曾尝试在 UserControls.Resources 中定义工具提示(文本块)的内容,然后设置文本块和 IsEnabled,但它给了我错误:
So, I want the tooltip to only show up when IsCharging is false. The issue that I am having is that because the Visibility property is on the tooltip textblock instead of the tooltip itself, setting it to not visible only gives me a empty tooltip, instead of the tooltip not appearing at all. I have tried defining the content of the tooltip (textblock) in UserControls.Resources and then setting the textblock and IsEnabled, but it gave me the error:
不能将 tooltipservice 类型的值添加到 inlinecolection 类型的集合或字典中
a value of type tooltipservice cannot be added to a collection or dictionary of type inlinecolection
似乎没有一种简单的方法可以设置工具提示的可见性.如果有人有任何建议,将不胜感激!
It doesn't seem there is an easy way to set the visibility for the tooltip. If anyone has any suggestions it would be greatly appreciated!
推荐答案
您可以使用 ToolTipService.IsEnabled
用于此目的的属性
You could use ToolTipService.IsEnabled
property for the purpose
ToolTipService.IsEnabled="{Binding IsToolTipVisible}"
Where IsToolTipVisible
视图模型属性在哪里,它决定了在哪里启用工具提示
Where IsToolTipVisible
Where is the View Model property which dictates where to enable the tooltip
完整代码
<TextBlock Grid.Row="2" ToolTipService.IsEnabled="{Binding IsToolTipVisible}"
Grid.Column="0"
VerticalAlignment="Center"
FontSize="15"
Visibility="{Binding IsConnected, Converter={StaticResource BooleanToVisibilityConverter}}">
<fa:ImageAwesome Icon="{Binding Path=BatteryLevelIcon, UpdateSourceTrigger=PropertyChanged}"
Height="20"
Width="20"
Foreground="Green"
Visibility="{Binding IsConnected, Converter={StaticResource BooleanToVisibilityConverter}}" />
<ToolTipService.ToolTip>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}%">
<Binding Path="BatteryPercentage" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</ToolTipService.ToolTip>
</TextBlock>
这篇关于使工具提示不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!