WPF - Tooltip on a disabled hyperlink with an embedded TextBlock element

时光怂恿深爱的人放手 提交于 2019-12-24 08:48:06

问题


I'm trying to put tooltips on disabled hyperlinks in my WPF app. The hyperlinks have embedded TextBlock elements for Text parameter binding. However, for some reason tooltips don't work on disabled hyperlinks with an embedded TextBlock element. Here is an example:

<Grid>
    <StackPanel>
        <TextBlock TextAlignment="Center" Margin="5">
            <Hyperlink IsEnabled="False" ToolTip="ToolTip" ToolTipService.ShowOnDisabled="True">Text</Hyperlink>
        </TextBlock>
        <TextBlock TextAlignment="Center" Margin="5">
            <Hyperlink IsEnabled="True" ToolTip="ToolTip" ToolTipService.ShowOnDisabled="True">
                <TextBlock Text="Text"/>
            </Hyperlink>
        </TextBlock>
        <TextBlock TextAlignment="Center" Margin="5">
            <Hyperlink IsEnabled="False" ToolTip="ToolTip" ToolTipService.ShowOnDisabled="True">
                <TextBlock Text="Text"/>
            </Hyperlink>
        </TextBlock>
    </StackPanel>
</Grid>

This XAML describes three hyperlinks.

  • The first hyperlink is disabled, but has no embedded TextBlock element. The tooltip shows up fine.
  • The second hyperlink has an embedded TextBlock element, but is enabled. Again, the tooltip shows up fine.
  • The third hyperlink is disabled and has an embedded TextBlock element, which is what I need, but the tooltip is not shown.

What can I do to show tooltips on disabled hyperlinks with embedded TextBlock elements? I don't want to add the tooltip to the parent TextBlock, because I want the tooltip to only appear on the hyperlink text, and not the whole TextBlock area.

Thanks.


回答1:


I know it sounds strange but this seems to work:

<TextBlock Text="Hello there" IsEnabled="False">
    <Hyperlink ToolTip="ToolTip" ToolTipService.ShowOnDisabled="True">
        <TextBlock Text="Text" />
    </Hyperlink>
</TextBlock>

i.e., you have to disable the parent TextBlock.




回答2:


Move Tooltip to textblock like this

<TextBlock TextAlignment="Center" Margin="5" ToolTip="ToolTip">
    <Hyperlink IsEnabled="False" ToolTipService.ShowOnDisabled="True">
        <TextBlock Text="Text"/>
    </Hyperlink>
</TextBlock>


来源:https://stackoverflow.com/questions/25280282/wpf-tooltip-on-a-disabled-hyperlink-with-an-embedded-textblock-element

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