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

Joh*_*ood 2 c# wpf textblock

这个问题(来自eandersson的答案)中,在a中使用了一个超链接TextBlock.我想做同样但在后面的代码 - 如何做到这一点?

链接示例:

<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;
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*kin 8

这是在中间添加带有可点击链接的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);
Run Code Online (Sandbox Code Playgroud)

编程方式在文本之间创建带有超链接的文本块

您可以使用相同的方法将addidng文本块用于容器.