Hyperlink in WPF

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 FlowDocument CreateAHyperlinkDynamically()

{

    // Create a FlowDocument

    FlowDocument doc = new FlowDocument();

 

    // Create a Paragraph and 3 Runs

    Paragraph p = new Paragraph();

    Run run1 = new Run("Hyperlink Sample ");

    Run run2 = new Run(" Hyperlink added");

    Run run3 = new Run("C# Corner ");

    // Create a Hyperlink and set NavigateUri

    Hyperlink hlink = 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;

}

Listing 1

Up Next
    Ebook Download
    View all
    Learn
    View all