Dynamically adding hyperlinks to a RichTextBox

PaulN picture PaulN · Feb 14, 2012 · Viewed 11.5k times · Source

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!");
}

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.

Thanks.

Answer

H.B. picture H.B. · Feb 14, 2012

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