How to make a simple hyperlink in XAML? How to make a simple hyperlink in XAML? wpf wpf

How to make a simple hyperlink in XAML?


You can't add a Hyperlink to a StackPanel -- you'll get a runtime error. (Actually, I'm kinda surprised it's not a compile-time error.) That's because Hyperlink doesn't live in the "controls" side of WPF with <Button> and <StackPanel> and other things that are laid out on rectangular chunks of screen and descend from UIElement. Instead, it lives in the "text" side of things, with <Bold> and <Run> and <Paragraph> and other generally texty things that word-wrap and flow in lines and paragraphs and descend from TextElement.

Once you realize that there are two separate class hierarchies with different layout behaviors, it makes sense that Hyperlink would be on the "text" side of things (makes it easy to e.g. have a paragraph with a hyperlink in the middle, and even for that hyperlink to wrap across a line break).

But no, it's not so discoverable when you're starting out.

To mix the two worlds, and use a hyperlink as a control, all you need to do is put it in a TextBlock. TextBlock is a control-ish thing (i.e., can go in a StackPanel) that contains text-ish things (i.e., can contain a Hyperlink):

<TextBlock><Hyperlink Click="buttonClose_Click">Close</Hyperlink></TextBlock>


You can use a Button with a custom control template, the code below is a limited hyperlink style button (for example it only support textual hyperlinks) but maybe it'll point you in the right direction.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><Page.Resources><Style x:Key="Link" TargetType="Button">    <Setter Property="VerticalAlignment" Value="Center"/>    <Setter Property="HorizontalAlignment" Value="Center"/>    <Setter Property="Cursor" Value="Hand"/>    <Setter Property="Foreground" Value="Blue"/>    <Setter Property="Background" Value="Transparent"/>    <Setter Property="Template">        <Setter.Value>            <ControlTemplate TargetType="Button">                <TextBlock TextDecorations="Underline"                     Text="{TemplateBinding Content}"                    Background="{TemplateBinding Background}"/>                <ControlTemplate.Triggers>                    <Trigger Property="IsPressed" Value="True">                        <Setter Property="Foreground" Value="Red"/>                    </Trigger>                </ControlTemplate.Triggers>            </ControlTemplate>        </Setter.Value>    </Setter></Style></Page.Resources><Button Content="Click Me!" Style="{StaticResource Link}"/></Page>


Try this:

<TextBlock>    <Hyperlink RequestNavigate="Hyperlink_RequestNavigate"                NavigateUri="http://www.msn.com">MSN</Hyperlink> </TextBlock>

private void Hyperlink_RequestNavigate(object sender,                                       System.Windows.Navigation.RequestNavigateEventArgs e){    System.Diagnostics.Process.Start(e.Uri.AbsoluteUri);}