【问题标题】:How to add a hyperlink in a TextBlock in code?如何在代码中的 TextBlock 中添加超链接?
【发布时间】:2014-01-19 16:05:07
【问题描述】:

this question(来自 eandersson 的答案)中,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;
}

【问题讨论】:

    标签: c# wpf textblock


    【解决方案1】:

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

    来自programmatically make textblock with hyperlink in between text

    您可以使用相同的方式将文本块添加到容器中。

    【讨论】:

      猜你喜欢
      • 2011-10-15
      • 2015-06-23
      • 1970-01-01
      • 1970-01-01
      • 2010-10-06
      • 1970-01-01
      • 2014-06-18
      • 2011-03-24
      • 2015-07-08
      相关资源
      最近更新 更多