WPF中工具提示或弹出窗口上的可点击超链接

时间:2016-02-23 16:51:30

标签: c# wpf xaml tooltip

我需要在工具提示上显示超链接。超链接应该是可点击的。到目前为止,我的XAML是:

<Button Content="Click..!!"
        Height="23"
        HorizontalAlignment="Left"
        Margin="191,108,0,0"
        Name="button1"
        VerticalAlignment="Top"
        Width="75" >
    <Button.ToolTip>
        <ToolTip StaysOpen="True"
                 ToolTipService.BetweenShowDelay="5000"
                 ToolTipService.ShowDuration="5000"
                 ToolTipService.InitialShowDelay="5000">
            <Hyperlink NavigateUri="http://stackoverflow.com/questions/">http://stackoverflow.com/questions/</Hyperlink>
        </ToolTip>
    </Button.ToolTip>
</Button>

输出:

the output

但它无法点击并立即隐藏。我需要链接可点击。

1 个答案:

答案 0 :(得分:5)

您必须使用Popup滚动自己的“工具提示”。工具提示并非设计为交互式,因为您正在请求,但弹出窗口可以提供对显示功能的更多控制。

然后,您可以通过工具提示服务提供的类似事件(鼠标悬停,鼠标离开等)来控制弹出窗口。

<Button x:Name="bttnTarget" MouseEnter="bttnTarget_MouseEnter" Content="Click..!!" Height="23" HorizontalAlignment="Left" Margin="191,108,0,0" VerticalAlignment="Top" Width="75" />
<Popup x:Name="tooltip" PlacementTarget="{Binding ElementName=bttnTarget}" MouseLeave="bttnTarget_MouseLeave" Placement="Bottom">
   <StackPanel>
      <TextBlock>
         <Hyperlink NavigateUri="http://stackoverflow.com/questions/">http://stackoverflow.com/questions/</Hyperlink>
      </TextBlock>
   </StackPanel>
</Popup>

背后的代码只是在按钮上使用移动器并从弹出窗口中退出。您必须推导出符合用户要求的显示方法。

    private void bttnTarget_MouseLeave(object sender, MouseEventArgs e)
    {
        tooltip.IsOpen = false;
    }
private void bttnTarget_MouseEnter(object sender, MouseEventArgs e)
{
        tooltip.IsOpen = true;
    }