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.

  1. <Hyperlink>  
  2.    Inlines  
  3. </Hyperlink >  

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

  1. <Hyperlink NavigateUri="http://www.c-sharpcorner.com">  
  2.    C# Corner  
  3. </Hyperlink>  
Listing 1 is a complete example that shows how to use a Hyperlink element in  FlowDocument contents.

  1. <FlowDocument ColumnWidth="400" IsOptimalParagraphEnabled="True" IsHyphenationEnabled="True">  
  2.     <Section Name="Heading" FontSize="20" FontFamily="Georgia" Background="LightSalmon">  
  3.         <Paragraph> Hyperlink sample </Paragraph>  
  4.     </Section>  
  5.     <Section FontSize="12">  
  6.         <Paragraph>  
  7.             <Bold>Mindcracker Network</Bold> including <Bold>  
  8.                 <Hyperlink NavigateUri="http://www.c-sharpcorner.com"> C# Corner </Hyperlink>  
  9.             </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>  
  10.             <Floater Background="GhostWhite" Width="285" HorizontalAlignment="Left">  
  11.                 <Table CellSpacing="5">  
  12.                     <Table.Columns>  
  13.                         <TableColumn Width="155" />  
  14.                         <TableColumn Width="130" />  
  15.                     </Table.Columns>  
  16.                     <TableRowGroup>  
  17.                         <TableRow>  
  18.                             <TableCell ColumnSpan="3" Background="LightBlue" FontSize="16">  
  19.                                 <Paragraph>Mindcracker Statistics</Paragraph>  
  20.                             </TableCell>  
  21.                         </TableRow>  
  22.                         <TableRow Background="LightGoldenrodYellow" FontSize="11">  
  23.                             <TableCell>  
  24.                                 <Paragraph FontWeight="Bold">Monthly Page views:</Paragraph>  
  25.                             </TableCell>  
  26.                             <TableCell>  
  27.                                 <Paragraph>3.8 Million</Paragraph>  
  28.                             </TableCell>  
  29.                         </TableRow>  
  30.                         <TableRow FontSize="11" Background="LightGray">  
  31.                             <TableCell>  
  32.                                 <Paragraph FontWeight="Bold">Monthly Unique Visitors:</Paragraph>  
  33.                             </TableCell>  
  34.                             <TableCell>  
  35.                                 <Paragraph>2.3 Million</Paragraph>  
  36.                             </TableCell>  
  37.                         </TableRow>  
  38.                         <TableRow Background="LightGoldenrodYellow" FontSize="11">  
  39.                             <TableCell>  
  40.                                 <Paragraph FontWeight="Bold">US Visitors:</Paragraph>  
  41.                             </TableCell>  
  42.                             <TableCell>  
  43.                                 <Paragraph>43%</Paragraph>  
  44.                             </TableCell>  
  45.                         </TableRow>  
  46.                         <TableRow>  
  47.                             <TableCell ColumnSpan="4">  
  48.                                 <Paragraph FontSize="10" FontStyle="Italic"> View more details on <Hyperlink NavigateUri="http://www.c-sharpcorner.com/forums/"> Mindcracker Network. </Hyperlink>  
  49.                                 </Paragraph>  
  50.                             </TableCell>  
  51.                         </TableRow>  
  52.                     </TableRowGroup>  
  53.                 </Table>  
  54.             </Floater>  
  55.         </Paragraph>  
  56.     </Section>  
  57. </FlowDocument>  

Listing 1

The output of Listing 1 looks like Figure 1.

Hyperlink In WPF
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.

  1. privateFlowDocument CreateAHyperlinkDynamically() {  
  2.     // Create a FlowDocument  
  3.     FlowDocument doc = newFlowDocument();  
  4.     // Create a Paragraph and 3 Runs  
  5.     Paragraph p = newParagraph();  
  6.     Run run1 = newRun("Hyperlink Sample ");  
  7.     Run run2 = newRun(" Hyperlink added");  
  8.     Run run3 = newRun("C# Corner ");  
  9.     // Create a Hyperlink and set NavigateUri  
  10.     Hyperlink hlink = newHyperlink(run3);  
  11.     hlink.NavigateUri = newUri("http://www.c-sharpcorner.com");  
  12.     // Add Runs and Hyperlink to Paragraph  
  13.     p.Inlines.Add(run1);  
  14.     p.Inlines.Add(hlink);  
  15.     p.Inlines.Add(run2);  
  16.     // Add Paragraph to FlowDocument  
  17.     doc.Blocks.Add(p);  
  18.     return doc;  
  19. }  

Listing 1


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.