Compartilhar via


Como: Use a Text Decoration with a Hyperlink

The Hyperlink object is an inline-level flow content element that allows you to host hyperlinks within the flow content. Por padrão, Hyperlink usa um TextDecoration o objeto para exibir um sublinhado. TextDecorationos objetos podem ser desempenho intensivo, para criar uma instância, especialmente se você tiver muitos Hyperlink objetos. If you make extensive use of Hyperlink elements, you may want to consider showing an underline only when triggering an event, such as the MouseEnter event.

In the following example, the underline for the "My MSN" link is dynamic—it only appears when the MouseEnter event is triggered.

Hyperlinks defined with TextDecorations

Hiperlinks exibindo TextDecorations

Exemplo

The following markup sample shows a Hyperlink defined with and without an underline:

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

<Run Text=" | " />

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

The following code sample shows how to create an underline for the Hyperlink on the MouseEnter event, and remove it on the MouseLeave event.

        ' 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
// 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;
}

Consulte também

Tarefas

Como: Criar um enfeite de texto

Referência

TextDecoration

Hyperlink

Conceitos

Optimizing WPF Application Performance