将超链接动态添加到 RichTextBox [英] Dynamically adding hyperlinks to a RichTextBox

查看:35
本文介绍了将超链接动态添加到 RichTextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 WPF 和 C# 向 RichTextBox 动态添加一些超链接,但没有取得多大成功.我的代码总结如下:

I'm trying to dynamically add some hyperlinks to a RichTextBox using WPF and C# but am not having much success. My code is summarised below:

FlowDocument doc = new FlowDocument();
richTextBox1.Document = doc;
richTextBox1.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");
link.Click += new RoutedEventHandler(this.link_Click);
para.Inlines.Add(link);

....

protected void link_Click(object sender, RoutedEventArgs e) {
    MessageBox.Show("Clicked link!");
}

当我运行这个时,RichTextBox 会显示链接,但它是灰色的,我无法点击它?有人可以指出我可能出错的地方.

When I run this the RichTextBox show the link but it is grey and I cannot click on it? Can someone please point out where I might be going wrong.

谢谢.

推荐答案

RichTextBox 中的 Document 默认是禁用的,设置 RichtTextBox.IsDocumentEnabledtrue.

The Document in a RichTextBox is disabled by default, set RichtTextBox.IsDocumentEnabled to true.

这篇关于将超链接动态添加到 RichTextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆