[英]Add clickable hyperlinks to a RichTextBox without new paragraph
Is it possible to dynamically add hyperlinks without creating new paragraphs like in this question Dynamically adding hyperlinks to a RichTextBox ? 是否可以在不创建新段落的情况下动态添加超链接,例如在此问题中动态添加到RichTextBox的超链接 ?
I want something like "Please visit http://www.google.com . Thank you!" 我想要类似“请访问http://www.google.com 。谢谢!”之类的内容。 not
不
"Please visit “请拜访
http://www.google.com http://www.google.com
.Thank you!". 。谢谢!”。
Also RichTextBox must be readonly, user cannot type in it. 并且RichTextBox必须是只读的,用户不能键入它。 It's something like log, all I need is to periodically add some text which sometimes contains URLs.
就像日志一样,我需要定期添加一些有时包含URL的文本。
OK, looks like here is what I need (thanks @Blam and @PaulN Dynamically adding hyperlinks to a RichTextBox ): 好,看起来这就是我需要的(感谢@Blam和@PaulN 动态地将超链接添加到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? 但是还有一个小问题:也许有人知道如何删除开头的空白,该空白在上面的图像中用红线标记了吗?
As for making a RichTextBox or TextBox read only 至于使RichTextBox或TextBox只读
TextBoxBase.IsReadOnly Property TextBoxBase.IsReadOnly属性
For adding text you can use a run 要添加文本,您可以使用运行
FlowDocument doc = new FlowDocument();
rtb.Document = doc;
rtb.IsReadOnly = true;
Paragraph para = new Paragraph();
doc.Blocks.Add(para);
Hyperlink link = new Hyperlink();
link.IsEnabled = true;
link.Inlines.Add("Hyperlink");
link.NavigateUri = new Uri("http://www.google.co.uk");
para.Inlines.Add(link);
Run run = new Run();
run.Text = " next words";
para.Inlines.Add(run);
You can do it with 你可以做到
<ContentControl>
<Span>
<Run Text="Please visit"/>
<Hyperlink NavigateUri="http://google.com">
<Run Text="google"/>
</Hyperlink>
<Run Text=". Thank you!"/>
</Span>
</ContentControl>
And if you are in a navigationFrame you get the hyperlink functionality for free 如果您在navigationFrame中,则可以免费获得超链接功能
Or... 要么...
<StackPanel Orientation="Horizontal">
<TextBlock Text="Please visit"/>
<Button Style="linkButton" Content="Google" Command/Click="GotoGoogle"/>
<TextBlock Text=". Thank you!"/>
</StackPanel>
Note: To Remove the Blank Line from the RichText by doing the following: 注意:通过执行以下操作,从RichText中删除空白行:
MyRichTextBox.Document.Blocks.Clear();
move blank space at the beginning of RichTextBox as you add Paragraph Runs 添加段落运行时,在RichTextBox的开头移动空白
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.