Alessandro Del Sole's Blog

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

My Links

News

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 2012

Order my book about VB 2012 on Amazon My book "Visual Basic 2012 Unleashed" is available. Click the cover!

My new book on LightSwitch!

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

Your visits

campusMVP.NET - Tutored online training for Microsoft developers

Follow me on Twitter!

Messenger me!


CyberInstaller Beta Tester

Download CIS 2008!!

CodePlex download Download my open-source projects from CodePlex!

Search the blog



Article Categories

Archives

Post Categories

.NET Framework

Blogroll

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="https://mvp.support.microsoft.com/profile/Alessandro.Del%20Sole"

                       Hyperlink.RequestNavigate="Hyperlink_RequestNavigate" >

                <TextBlock Text="Alessandro Del Sole" />

            </Hyperlink>

        </Label>

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 _

                                                System.Windows.Navigation.RequestNavigateEventArgs)

        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">

            <Label.Resources>

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

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

                <Style.Triggers>

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

                        <Setter Property="Foreground">

                            <Setter.Value>

                                <LinearGradientBrush>

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

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

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

                                </LinearGradientBrush>

                            </Setter.Value>

                        </Setter>

                    </Trigger>

                </Style.Triggers>

            </Style>

            </Label.Resources>

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

            <Hyperlink NavigateUri="https://mvp.support.microsoft.com/profile/Alessandro.Del%20Sole"

                       Hyperlink.RequestNavigate="Hyperlink_RequestNavigate"

                       Style="{DynamicResource HyperStyle}">

                <TextBlock Text="Alessandro Del Sole" />

            </Hyperlink>

        </Label>

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.

Alessandro

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

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 7 and 5 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET