简体   繁体   English

如何在代码中的TextBlock中添加超链接?

[英]How to add a hyperlink in a TextBlock in code?

In this question (the answer from eandersson) a hyperlink is used within a TextBlock . 这个问题 (来自eandersson的答案)中,在TextBlock使用了超链接。 I would like to do the same but in the code behind - how to do that? 我想做同样但在后面的代码 - 如何做到这一点?

Example from the link: 链接示例:

<TextBlock>           
    <Hyperlink NavigateUri="http://www.google.com" RequestNavigate="Hyperlink_RequestNavigate">
        Click here
    </Hyperlink>
</TextBlock>

private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
    Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
    e.Handled = true;
}

Here's the code to add a TextBlock with a clickable link in the middle : 这是在中间添加带有可点击链接的TextBlock的代码:

Run run1 = new Run("click ");
Run run2 = new Run(" Please");
Run run3 = new Run("here.");

Hyperlink hyperlink = new Hyperlink(run3)
                       {
                           NavigateUri = new Uri("http://stackoverflow.com")
                       };
hyperlink.RequestNavigate += new System.Windows.Navigation.RequestNavigateEventHandler(hyperlink_RequestNavigate); //to be implemented
textBlock1.Inlines.Clear();
textBlock1.Inlines.Add(run1);
textBlock1.Inlines.Add(hyperlink);
textBlock1.Inlines.Add(run2);

from programmatically make textblock with hyperlink in between text 编程方式在文本之间创建带有超链接的文本块

The same way you can use for addidng text block to container. 您可以使用相同的方法将addidng文本块用于容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM