When an end-user clicks a Hyperlink in a WPF application it does not open a web browser to the value specified in the NavigateUri property. For this to occur, it is the developer's responsibility to add an event handler for the Hyperlink.RequestNavigate event and then launch a browser to the desired URI.
This functionality can be encapsulated in a control by subclassing Hyperlink, as shown in the following C# example.
public sealed class ExternalHyperlink : Hyperlink
{
public ExternalHyperlink()
{
this.RequestNavigate += NavigateByDefaultProcess;
}
private void NavigateByDefaultProcess(object sender, RequestNavigateEventArgs e)
{
Uri uri = this.NavigateUri;
if (uri != null)
{
if (!uri.IsAbsoluteUri)
throw new InvalidOperationException("An absolute URI is required.");
System.Diagnostics.Process.Start(uri.ToString());
}
}
}
To use this control in XAML you must first assign an XML prefix to the control's namespace using the xmlns attribute on the root element of your XAML file. In the following example, it is assumed that the control has been defined within the MyControlsNamespace namespace of an assembly named, MyControls.
<Window x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mycontrols="clr-namespace:MyControlsNamespace;assembly=MyControls"
Title="My App">
...
</Window>
Then you can use the ExternalHyperlink control in place of the Hyperlink control.
<mycontrols:ExternalHyperlink NavigateUri="http://www.wikipedia.org">Wiki info</mycontrols:ExternalHyperlink>