C# WPF-如何仅在禁用超链接时显示超链接上的工具提示

C# WPF-如何仅在禁用超链接时显示超链接上的工具提示,c#,wpf,xaml,hyperlink,tooltip,C#,Wpf,Xaml,Hyperlink,Tooltip,这是对我的问题的更具体的描述,并附有后续的答案 我在XAML中定义了一个标准超链接: <TextBlock> <Hyperlink IsEnabled="{Binding LinkEnabled}"> <TextBlock Text="{Binding Text}"/> </Hyperlink> </TextBlock> 超链接的IsEnabled属性绑定到视图模型上的属性,该属性的值可以更改。我需

这是对我的问题的更具体的描述,并附有后续的答案

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

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


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

要仅在超链接被禁用时显示工具提示


但是,工具提示不会显示,因为一旦超链接被禁用,因为它包含在TextBlock中(或者我所理解的)

因此,解决方案是更改父TextBlock上的“IsEnabled”属性,而不是超链接上的属性。 然而,这是可行的:

<TextBlock IsEnabled="False">

但这并不是:

<TextBlock IsEnabled="{Binding LinkEnabled}">

在后一种情况下。为了解决这个问题

这里是实际的解决方案,全部放在一起:

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

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