WPF - 仅当禁用超链接时如何在超链接上显示工具提示 [英] WPF - How to display a tooltip on a hyperlink only when the hyperlink is disabled

查看:398
本文介绍了WPF - 仅当禁用超链接时如何在超链接上显示工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对我的问题的更具体描述来自上一个问题,后续答案。

This is a more specific description of my problem from a previous question, with a follow-up answer.

我在XAML中定义了标准超链接:

I had a standard hyperlink defined in XAML:

<TextBlock>
    <Hyperlink IsEnabled="{Binding LinkEnabled}">
        <TextBlock Text="{Binding Text}"/>
    </Hyperlink>
</TextBlock>

超链接的IsEnabled属性绑定到视图模型上的属性,其值可以更改。我需要在超链接上放置工具提示,只有在超链接被禁用时才会显示。

The hyperlink's IsEnabled property is bound to a property on the view model, the value of which can change. I needed to place a tooltip on the hyperlink which would only be displayed if the hyperlink is disabled.

推荐答案

仅显示工具提示禁用超链接时,必须在超链接上设置 ToolTipService.ShowOnDisabled和ToolTipService.IsEnabled(带有否定转换器)属性:

To show the tooltip only when the hyperlink is disabled, the ToolTipService.ShowOnDisabled and ToolTipService.IsEnabled (with the negation converter) properties must be set on the hyperlink:

<TextBlock>
    <Hyperlink IsEnabled="{Binding LinkEnabled}"
               ToolTip="ToolTip"
               ToolTipService.ShowOnDisabled="True"
               ToolTipService.IsEnabled="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={StaticResource negateConverter}}">
        <TextBlock Text="{Binding Text}"/>
    </Hyperlink>
</TextBlock>

但是,工具提示将不会显示,因为一旦超链接被禁用,停止受到攻击测试,因为它包含在TextBlock中(或者我理解)。

However, the tooltip won't be shown, since once the hyperlink is disabled, it stops being hit-testable, because it is contained in the TextBlock (or so I understood).

因此,解决方案是更改父TextBlock上的IsEnabled属性,而不是超链接。
但是,这有效:

Thus, the solution would be to change the "IsEnabled" property on the parent TextBlock, not on the hyperlink. However, this works:

<TextBlock IsEnabled="False">

但这不是:

<TextBlock IsEnabled="{Binding LinkEnabled}">

在后一种情况下,更改TextBlock的IsEnabled属性不会更改超链接的IsEnabled属性。要解决此问题,超链接的IsEnabled属性必须绑定到父级的属性

In the latter case, changing the TextBlock's IsEnabled property will not change the hyperlink's IsEnabled property. To solve this issue, the hyperlink's IsEnabled property must be bound to the parent's property.

以下是实际解决方案,全部放在一起:

<TextBlock IsEnabled="{Binding LinkEnabled}">
    <Hyperlink IsEnabled="{Binding IsEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type FrameworkElement}}}"
               ToolTip="ToolTip"
               ToolTipService.ShowOnDisabled="True"
               ToolTipService.IsEnabled="{Binding IsEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type FrameworkElement}}, Converter={StaticResource negateConverter}}">
        <TextBlock Text="{Binding Text}"/>
    </Hyperlink>
</TextBlock>

这篇关于WPF - 仅当禁用超链接时如何在超链接上显示工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆