点击超链接添加到没有新的段落一个RichTextBox [英] Add clickable hyperlinks to a RichTextBox without new paragraph
问题描述
是否可以动态地添加超链接没有在这个问题上动态添加超链接一个RichTextBox ?
Is it possible to dynamically add hyperlinks without creating new paragraphs like in this question Dynamically adding hyperlinks to a RichTextBox?
我想要的东西,如请访问 http://www.google .COM 。谢谢你!不是
I want something like "Please visit http://www.google.com. Thank you!" not
请访问
。谢谢!
此外RichTextBox的必须是只读的,用户不能在它的类型。这有点像日志,我需要的是定期添加一些文字,有时会包含的URL。
Also RichTextBox must be readonly, user cannot type in it. It's something like log, all I need is to periodically add some text which sometimes contains URLs.
推荐答案
OK,貌似这里是我所需要的(感谢@Blam和@PaulN的Dynamically添加超链接到一个RichTextBox ):
OK, looks like here is what I need (thanks @Blam and @PaulN Dynamically adding hyperlinks to a RichTextBox):
public MainWindow()
{
InitializeComponent();
rtb.IsDocumentEnabled = true;
rtb.Document.Blocks.FirstBlock.Margin = new Thickness(0);
}
private void AddHyperlinkText(string linkURL, string linkName,
string TextBeforeLink, string TextAfterLink)
{
Paragraph para = new Paragraph();
para.Margin = new Thickness(0); // remove indent between paragraphs
Hyperlink link = new Hyperlink();
link.IsEnabled = true;
link.Inlines.Add(linkName);
link.NavigateUri = new Uri(linkURL);
link.RequestNavigate += (sender, args) => Process.Start(args.Uri.ToString());
para.Inlines.Add(new Run("[" + DateTime.Now.ToLongTimeString() + "]: "));
para.Inlines.Add(TextBeforeLink);
para.Inlines.Add(link);
para.Inlines.Add(new Run(TextAfterLink));
rtb.Document.Blocks.Add(para);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
AddHyperlinkText("http://www.google.com", "http://www.google.com",
"Please visit ", ". Thank you! Some veeeeeeeeeery looooooong text.");
}
不过一个小问题留给:也许有人知道如何在标有开始删除空格上面的图片红线?
But one little problem left: maybe someone know how to remove blank space at the beginning which is marked with the red line on the image above?
这篇关于点击超链接添加到没有新的段落一个RichTextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!