SIGN UP MEMBER LOGIN:    
ARTICLE

Hyperlink in WPF

Posted by Mahesh Chand Articles | WPF March 07, 2010
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.
Reader Level:

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

share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor