WPF comes with a HyperLink class, but it’s from the System.Windows.Documents namespace, so is mean’t to be embedded within a textual control, such as a TextBlock (i.e. it’s not a standalone control like the System.Windows.Forms.LinkLabel) and within WPF it doesn’t support opening the supplied Uri in an associated process.
Let’s see it’s usage in some XAML
<TextBlock> <Hyperlink NavigateUri="http://putridparrot.com/blog/the-wpf-hyperlink"> The WPF Hyperlink </Hyperlink> </TextBlock>
This XAML will display a the text “The WPF Hyperlink” with underline and when you move your mouse over the link it’ll change colour and the mouse cursor will change to the hand cursor, but clicking on the link will not result in any web browser (or other associated application opening). We must implement such functionality ourselves either responding to the Click event or supplying an ICommand to the Command property.
Here’s a simple example of the sort of code we’d use to running the Uri via it’s associated application.
var process = new Process { StartInfo = new ProcessStartInfo(Uri) }; process.Start();
We can use databinding to supply the NavigateUri in the standard way, but if you wanted to change the displayed text, then we need to embed another control into the Hyperlink. For example, here we’re using another class from the System.Windows.Documents namespace, the Run class
<TextBlock> <Hyperlink NavigateUri="{Binding Uri}"> <Run> <Run.Text> <Binding Path="DisplayText"/> </Run.Text> </Run> </Hyperlink> </TextBlock>