I load WPF Richtextbox contents from Xaml string in which there are some Hyperlinks. When it is loaded into control, Hyperlinks are not clickable! I want to click on them and their associated URL shows up.
-
2use this: `richTextBox.IsDocumentEnabled = true;` – Ernesto Alfonso Jun 28 '16 at 15:05
3 Answers
No freschx, it's about WPF. A WPF RichTextBox
, unlike the one in WinForms, does not have a DetectUrls
property. And it's weird you wrote a Xaml code for that, even weirder there is someone who thought it useful.
Check this post out where JHubbard80 and me had two different approaches to solve this problem.
To make the Hyperlink
, or any inline UIElement
in general, available for hit testing, we must set RichTextBox.IsDocumentEnabled
to true
.
To make the Hyperlink
clickable without pressing the Ctrl key, the Hyperlink
must be made read-only e.g., by wrapping it into a TextBlock
or by making the complete RichTextBox
read-only (by setting RichTextBox.IsReadOnly
to false
).
<RichTextBox IsDocumentEnabled="True">
<FlowDocument>
<Paragraph>
<Run Text="Some editable text" />
<TextBlock>
<Hyperlink NavigateUri="https://duckduckgo.com">
DuckDuckGo
</Hyperlink>
</TextBlock>
</Paragraph>
</FlowDocument>
</RichTextBox>

- 1
- 4
- 28
- 44
Ensure the DetectUrls
property on the RichTextbox is set to true. You can then attach an event handler to the link clicked event and do what you wish.
<RichTextBox DetectUrls="True" />
Potential duplicate thread. Credit goes to Sam Meldrum from this thread.
For an even deeper analysis you may wish to try this article.
-
7You're wrong, the property DetectUrls is related to WinForms' RichTextBox, but not to WPF's one. – white.zaz Jun 27 '14 at 13:18