如何:指定超链接是否带下划线

Hyperlink 对象是一个内联级流内容元素,可用于在流内容中托管超链接。 默认情况下,Hyperlink 使用 TextDecoration 对象来显示下划线。 TextDecoration 对象可能会消耗大量性能来实例化,尤其是在有许多 Hyperlink 对象的情况下。 如果广泛使用 Hyperlink 元素,则可能需要仅在触发事件(如 MouseEnter 事件)时考虑显示下划线。

在以下示例中,“我的 MSN”链接的下划线是动态的,即仅在触发 MouseEnter 事件时才显示。

显示 TextDecoration 的超链接

以下标记示例演示使用和不使用下划线定义的 Hyperlink

<!-- Hyperlink with default underline. -->
<Hyperlink NavigateUri="http://www.msn.com">
  MSN Home
</Hyperlink>

<Run Text=" | " />

<!-- Hyperlink with no underline. -->
<Hyperlink Name="myHyperlink" TextDecorations="None"
           MouseEnter="OnMouseEnter"
           MouseLeave="OnMouseLeave"
           NavigateUri="http://www.msn.com">
  My MSN
</Hyperlink>

下面的代码示例演示如何为 MouseEnter 事件上的 Hyperlink 创建下划线,并在 MouseLeave 事件上将其删除。

// Display the underline on only the MouseEnter event.
private void OnMouseEnter(object sender, EventArgs e)
{
    myHyperlink.TextDecorations = TextDecorations.Underline;
}

// Remove the underline on the MouseLeave event.
private void OnMouseLeave(object sender, EventArgs e)
{
    myHyperlink.TextDecorations = null;
}
' Display the underline on only the MouseEnter event.
Private Overloads Sub OnMouseEnter(ByVal sender As Object, ByVal e As EventArgs)
    myHyperlink.TextDecorations = TextDecorations.Underline
End Sub

' Remove the underline on the MouseLeave event.
Private Overloads Sub OnMouseLeave(ByVal sender As Object, ByVal e As EventArgs)
    myHyperlink.TextDecorations = Nothing
End Sub

另请参阅