WPF_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;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值