4

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.

Alchi
  • 799
  • 9
  • 19

3 Answers3

9

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.

Community
  • 1
  • 1
hillin
  • 1,603
  • 15
  • 21
1

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>
BionicCode
  • 1
  • 4
  • 28
  • 44
-17

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.

Community
  • 1
  • 1
freschx
  • 94
  • 1
  • 1
  • 11