C# Hyperlink in TextBlock: nothing happens when I click on it

Nicolas Raoul picture Nicolas Raoul · Oct 5, 2012 · Viewed 12.1k times · Source

In my C# standalone application, I want to let users click on a link that would launch their favorite browser.

System.Windows.Controls.TextBlock text = new TextBlock();
Run run = new Run("Link Text");

Hyperlink link = new Hyperlink(run);
link.NavigateUri = new Uri("http://w3.org");
text.Inlines.Add(link);

The link is displayed correctly.

When I move the mouse over it, the link becomes red.

PROBLEM: When I click it, nothing happens.

Did I forget something? Do I need to implement some kind of method to really let the link be opened?

Answer

markmuetz picture markmuetz · Oct 5, 2012

You need to handle the hyperlink's RequestNavigate event. Here's a quick way of doing it:

link.RequestNavigate += (sender, e) =>
{
    System.Diagnostics.Process.Start(e.Uri.ToString());
};