WPF Hyperlink in VB.NET

The Hyperlink element is an inline-level content element that is used to add a hyperlink to a FlowDocument contents. You can add hyperlink support to any Inline element.
  • 2051

The Hyperlink element is an inline-level content element that is used to add a hyperlink to a FlowDocument contents. You can add hyperlink support to any Inline element.

Hyperlink is defined in System.Windows.Documents namespace. You must import this namespace before you use Hyperlink.

Here is the Italic syntax for Hyperlink. 

<Hyperlink>
  Inlines
</Hyperlink >

Here is an example of hyperlink that sets NavigateUri to C# Corner website URL.

<Hyperlink NavigateUri="http://www.c-sharpcorner.com">
    C# Corner
</Hyperlink>

Listing 1 is a complete example that shows how to use a Hyperlink element in a FlowDocument contents.

<FlowDocument ColumnWidth="400" IsOptimalParagraphEnabled="True" IsHyphenationEnabled="True"   >
    <Section Name="Heading" FontSize="20" FontFamily="Georgia" Background="LightSalmon"  >
        <Paragraph>
            Hyperlink sample
       
</Paragraph>

    </Section>
    <Section FontSize="12">
        <Paragraph>
            <Bold>Mindcracker Network</Bold> including
           
<Bold>
                <Hyperlink NavigateUri="http://www.c-sharpcorner.com">
                    C# Corner
               
</Hyperlink>
            </Bold>

            is an online community for Software developers and profressioals.
    Mindcracker Network allows its members to share their knowlege among
    one another via its contents publishing including
           
<Bold>Articles, Blogs, Tutorials, and Videos.</Bold> It also allows members to
    ask and reply questions on
           
<Bold>Mindcracker Forums.</Bold>
           
<Floater
          Background="GhostWhite"
          Width="285" HorizontalAlignment="Left"
          >
                <Table CellSpacing="5">
                    <Table.Columns>
                        <TableColumn Width="155"/>
                        <TableColumn Width="130"/>
                    </Table.Columns>

                    <TableRowGroup>
                        <TableRow>
                            <TableCell ColumnSpan="3" Background="LightBlue" FontSize="16"  >
                                <Paragraph>Mindcracker Statistics</Paragraph>
                            </TableCell>
                        </TableRow>
 
                        <TableRow Background="LightGoldenrodYellow" FontSize="11">
                            <TableCell>
                                <Paragraph FontWeight="Bold">Monthly Page views:</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>3.8 Million</Paragraph>
                            </TableCell>
                        </TableRow>

                        <TableRow FontSize="11" Background="LightGray">
                            <TableCell>
                                <Paragraph FontWeight="Bold">Monthly Unique Visitors:</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>2.3 Million</Paragraph>
                            </TableCell>
                        </TableRow>

                        <TableRow Background="LightGoldenrodYellow" FontSize="11">
                            <TableCell>
                                <Paragraph FontWeight="Bold">US Visitors:</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>43%</Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow>
                            <TableCell ColumnSpan="4">
                                <Paragraph FontSize="10" FontStyle="Italic">
                                    View more details on
                                   
<Hyperlink NavigateUri="http://www.c-sharpcorner.com/forums/">
                                        Mindcracker Network.
                                   
</Hyperlink>
                                </Paragraph>
                            </TableCell>
                        </TableRow>
                    </TableRowGroup>
                </Table>
            </Floater>
        </Paragraph>

    </Section>
</FlowDocument>

Listing 1

The output of Listing 1 looks like Figure 1.

 

hyperlinkImg.jpg

Figure 1

Dynamic Hyperlink

The Hyperlink class in WPF is used to add a hyperlink to a FlowDocument. The code snippet in Listing 1 creates a hyperlink dynamically.

Private Function CreateAHyperlinkDynamically() As FlowDocument
    ' Create a FlowDocument
    Dim doc As New FlowDocument()
    ' Create a Paragraph and 3 Runs
    Dim p As New Paragraph()
    Dim run1 As New Run("Hyperlink Sample ")
    Dim run2 As New Run(" Hyperlink added")
    Dim run3 As New Run("C# Corner ")
    ' Create a Hyperlink and set NavigateUri
    Dim hlink As New Hyperlink(run3)
    hlink.NavigateUri = New Uri("http://www.c-sharpcorner.com")
    ' Add Runs and Hyperlink to Paragraph
    p.Inlines.Add(run1)
    p.Inlines.Add(hlink)
    p.Inlines.Add(run2)
    ' Add Paragraph to FlowDocument
    doc.Blocks.Add(p)
    Return doc
End Function

Listing 1

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.