WPF RichTextBox how to get it to accept clickable urls
Loading
WPF RichTextBox how to get it to accept clickable urls
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Raghunath BhukanRank #744 · 3.1K reputationPosted Jan 21, 2026, 6:45 AM EST
If you want a WPF
RichTextBoxwhere URLs in the text are automatically clickable links. By default,RichTextBoxjust treats text as plain content orFlowDocumentcontent — it won’t automatically make URLs clickable. You have to either detect URLs and convert them toHyperlinkelements, or handle mouse clicks manually.Here’s how to do it step by step.
Step 1: Use a
RichTextBoxwith aFlowDocumentImportant:
IsDocumentEnabled="True"allowsHyperlinkelements inside theRichTextBoxto be clickable.Step 2: Add text with URLs and convert them to
Hyperlinkprogrammaticallyusing System.Text.RegularExpressions; using System.Windows; using System.Windows.Documents; using System.Windows.Input; namespace ClickableUrlExample { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); AddTextWithClickableUrls("Visit https://www.example.com or http://google.com for more info."); } private void AddTextWithClickableUrls(string text) { var doc = new FlowDocument(); var paragraph = new Paragraph(); // Regex to find URLs var urlPattern = @"(http[s]?://[^\s]+)"; var parts = Regex.Split(text, urlPattern); foreach (var part in parts) { if (Regex.IsMatch(part, urlPattern)) { // Create a clickable hyperlink var hyperlink = new Hyperlink(new Run(part)) { NavigateUri = new System.Uri(part) }; hyperlink.RequestNavigate += Hyperlink_RequestNavigate; paragraph.Inlines.Add(hyperlink); } else { paragraph.Inlines.Add(new Run(part)); } } doc.Blocks.Add(paragraph); richTextBox.Document = doc; } private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e) { // Open the link in default browser System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(e.Uri.AbsoluteUri) { UseShellExecute = true }); e.Handled = true; } } }Step 3: Explanation
IsDocumentEnabled = trueis crucial for hyperlinks to work.The Regex detects URLs in the text.
Each URL is replaced with a
Hyperlinkelement.RequestNavigateevent handles the click and opens the URL in the default browser.Non-URL text is added as a normal
Runelement.Optional Enhancements
Automatically detect URLs while typing: Attach a
TextChangedhandler and re-parse the text to add hyperlinks.Styling hyperlinks: You can set
Foregroundcolor, underline style, etc.Support email links: Extend the regex to include
mailto:.