Making a ToolTip not visible

孤街醉人 提交于 2020-05-30 08:01:52

问题


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>

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:

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!


回答1:


You could use ToolTipService.IsEnabled property for the purpose

ToolTipService.IsEnabled="{Binding IsToolTipVisible}" 

Where IsToolTipVisible Where is the View Model property which dictates where to enable the tooltip

Complete Code

<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>


来源:https://stackoverflow.com/questions/60084835/making-a-tooltip-not-visible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!