如何:指定是否为超链接添加下划线

Hyperlink 对象为内联级流内容元素,允许承载流内容中的超链接。 默认情况下,Hyperlink 使用 TextDecoration 对象显示下划线。 实例化 TextDecoration 对象可能会占用大量性能,尤其是在拥有许多 Hyperlink 对象的情况下。 如果大范围使用 Hyperlink 元素,请考虑仅在触发事件时显示下划线,例如 MouseEnter 事件。

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

Hyperlinks displaying TextDecorations

示例

以下标记示例演示使用和不使用下划线定义的 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

另请参阅