以编程方式在文本块中添加超链接按钮

Adding hyperlink button in a text block programatically

如何通过文本末尾的 c# 在文本块中添加超链接按钮。 在 XAML 部分我有一个文本块

<TextBlock  Name="PresenterDescription" TextWrapping="Wrap"  Foreground="White" MinHeight="200" FontSize="16"/>

在 .cs 文件中我正在这样做..

Run run1 = new Run();
run1.Text = "some text";
HyperlinkButton hyperlinkButton = new HyperlinkButton()
{
    Content = " read more..",
    HorizontalAlignment = HorizontalAlignment.Left,
    NavigateUri = new Uri("http://somelink.com", UriKind.Absolute)
};
PresenterDescription.Inlines.Add(run1);

但是如何将超链接按钮添加到此文本块?,因为我无法将其添加为内联..

无法添加 HyperlinkBut​​ton,您可以将 HyperLink 添加到 TextBlock,或者您可以像这样使用 RichTextBlock

     <RichTextBlock x:Name="textblock"/>

 Run run1 = new Run();
run1.Text = "some text";
HyperlinkButton hyperlinkButton = new HyperlinkButton()
{
    Content = " read more..",
    HorizontalAlignment = HorizontalAlignment.Left,
    NavigateUri = new Uri("http://somelink.com", UriKind.Absolute)
};
Paragraph para = new Paragraph();
InlineUIContainer inline = new InlineUIContainer();
inline.Child = hyperlinkButton;
para.Inlines.Add(run1);
para.Inlines.Add(inline);
textblock.Blocks.Add(para);

XAML 部分我做了以下

<RichTextBox  Name="PresenterDescription"  VerticalAlignment="Top" FontSize="16">
            <Paragraph >
                       <Hyperlink Click="readMoreclick">
                            <Underline Foreground="White">read more..</Underline>
                      </Hyperlink>
             </Paragraph>
 </RichTextBox>

并且在 c# 部分中要在 link 之前显示的文本为..

PresenterDescription.Selection.Text = "Text to be displayed";

最后是 link 单击的事件处理程序..

  private void readMoreclick(object sender, RoutedEventArgs e)
         {
            WebBrowserTask webBrowserTask = new WebBrowserTask();
            webBrowserTask.Uri = new Uri("www.example.com", UriKind.Absolute);
            webBrowserTask.Show();
         }