Alessandro Del Sole's Blog

/* A programming space about Microsoft® .NET® */
posts - 159, comments - 0, trackbacks - 0

My Links


Your host

This is me! This space is about Microsoft® .NET® and Microsoft® Visual Basic development. Enjoy! :-)

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.

Microsoft MVP

My MVP Profile

I'm a VB!

Watch my interview in Seattle

My new book on VB 2015!

Pre-order VB 2015 Unleashed Pre-order my new book "Visual Basic 2015 Unleashed". Click for more info!

My new book on LightSwitch!

Visual Studio LightSwitch Unleashed My book "Visual Studio LightSwitch Unleashed" is available. Click the cover!

Your visits

Follow me on Twitter!

CodePlex download Download my open-source projects from CodePlex!

Article Categories


Post Categories

.NET Framework


Help Authoring

Microsoft & MSDN

Setup & Deployment

Visual Basic 2005/2008/2010

WPF: implementing a LinkLabel control with XAML and VB code

As you may know, Windows Presentation Foundation doesn't offer a LinkLabel control as instead happens in Windows Forms. By the way, creating a custom LinkLabel control in WPF is a simple task. This is because in WPF user controls are essentially the aggregation of primitive controls.

We can start from a Label control; its content will not be just text but an HyperLink control. Let's consider the following XAML code snippet, which reaches the objective sending to my MVP profile:

        <Label Width="150" Height="30">

            <Hyperlink NavigateUri=""

                       Hyperlink.RequestNavigate="Hyperlink_RequestNavigate" >

                <TextBlock Text="Alessandro Del Sole" />



We can decide the text that the Hyperlink must show via a TextBlock control setting its Text property. Regarding the HyperLink, the NavigateUri property must contain an Uri, such as an Internet address. This is possible because of the ContentPresenter, a special WPF control which allows to display any kind of item such as text, pictures, videos or other WPF controls like the Hyperlink, from within the control that exposes it.

The next step is providing an event handler for the RequestNavigate event. This event is raised each time the user clicks the link and it is generally used in client applications (not XBAP). We can handle the above mentioned event as follows:

    Private Sub Hyperlink_RequestNavigate(ByVal sender As System.Object, ByVal e As _


        Process.Start(New ProcessStartInfo(e.Uri.AbsoluteUri))


        e.Handled = True

    End Sub

Analyzing the e.Uri.AbsoluteUri property we can retrieve the URI associated to the HyperLink and we can send the link itself to the default browser invoking the Process.Start method. Notice that the System.Windows.Navigation.RequestNavigateEventArgs class belongs to the category of routed events; this is very useful because we can write a single event handler that will respond to all the Hyperlinks exposed by the user interface, sending to the browser the appropriate Uri related to the selected Hyperlink. Cool!

The above XAML produces the following output:

If we click the link, the default Web browser will open the related Web site. Moreover this custom LinkLabel can also be styled using WPF styles. For example, suppose you want to set the Hyperlink's foreground color to Violet and that when the mouse pointer passes over the link, the foreground color must change to a gradient. We could define a sample style as follows:

        <Label Width="150" Height="30">


            <Style TargetType="Hyperlink" x:Key="HyperStyle">

                <Setter Property="Foreground" Value="Violet"/>


                    <Trigger Property="IsMouseOver" Value="True">

                        <Setter Property="Foreground">



                                    <GradientStop Color="Orange" Offset="0"/>

                                    <GradientStop Color="Yellow" Offset="0.6"/>

                                    <GradientStop Color="Red" Offset="1"/>








that we can bind to the Hyperlink as in the following snippet:

            <Hyperlink NavigateUri=""


                       Style="{DynamicResource HyperStyle}">

                <TextBlock Text="Alessandro Del Sole" />



Now try and run the application to see what happens :-) By the way, the hierarchical logic of WPF controls makes very simple creating a custom LinkLabel.


Print | posted on giovedì 12 marzo 2009 19:10 | Filed Under [ Visual Basic Windows Presentation Foundation ]


No comments posted yet.

Post Comment

Please add 1 and 8 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET