Alessandro Del Sole's Blog

{ A programming space about Microsoft® .NET® }

  Home :: Contact :: Syndication  :: Login
  73 Posts :: 10 Stories :: 141 Comments :: 1729 Trackbacks

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 2010

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

Your visits

Windows Live Alerts

Vsi Builder 2008

My tool for VS 2005/2008 Download Vsi Builder, my dev tool for Visual Studio 2005/2008!

Add me to MSDN Social!

Follow me on Twitter!

Messenger me!


Grab this badge here!

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

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

posted on Thursday, March 12, 2009 5:10 PM

Feedback

# WPF: implementing a LinkLabel control with XAML and VB code | BlogBov 3/18/2009 12:35 AM Pingback/TrackBack
WPF: implementing a LinkLabel control with XAML and VB code | BlogBov

# re: WPF: implementing a LinkLabel control with XAML and VB code 3/21/2009 5:22 PM Marcel
Thank you for this post, I was looking for a hyperlink sample. But I run into problems when I change the content of the label through code label.content = "click here".

I guess this is because I overwrite the entire hyperlink definition. How can I change the text of the label through code without deleting the hyperlink style?

# re: WPF: implementing a LinkLabel control with XAML and VB code 3/21/2009 5:29 PM Alessandro Del Sole
Hi Marcel, you don't have to set the Label.Content property (this will override the entire control) but the TextBlock.Text property. Add a Name attribute to the TextBlock, then set the Text property in code.

Hope this helps

# re: WPF: implementing a LinkLabel control with XAML and VB code 3/21/2009 6:00 PM Marcel
Hi Alessandro, thank you for your quick response and the simple solution. As you have noticed, I am just starting with WPF last week. When googling for samples, I have come across your name and picture quite a few times. So I guess you have 'travelled' a simular path in WPF.

I have to tackle two more problems in the next week: 1) storing user preferences in ini format, 2)globalization/localization. I have found some a complex sample for the globalization (the coffee application), if you have simple solutions for those two subjects you would save me a lot of time. Thanks!

# re: WPF: implementing a LinkLabel control with XAML and VB code 3/29/2009 9:41 PM Alessandro Del Sole
Hi Marcel, sorry for the delay but I've been a little busy.

Regarding your questions:

1. .NET Framework offers Settings, and you can access Settings via the My.Settings object in Visual Basic code and set Settings at design time via the My Project window. This is a much better approach rather than Ini files.

2. Globalization is not a brief task to achieve. By the way, please try to take a look at the following links:

http://msdn.microsoft.com/en-us/library/ms788718.aspx

http://www.codeproject.com/KB/WPF/Localization_in_WPF.aspx

Hope this helps.

# re: WPF: implementing a LinkLabel control with XAML and VB code 3/30/2009 10:01 AM Marcel
Thanks for your reply. I've been researching and I have found some other links on codeproject as well (Josh). I will set up a small test project and see if I get it to work.

For the options I have found samples on how to use the application and user settings, but I am not sure it is the right way for me.

It seems perfect for some simple user settings like window size. But in my case I have approx 40-60 user preferences and I am not sure I trust the application settings to do the job properly.

So built-in application settings, ini file, XML file... I am stil not sure. In your experience is the built-in application settings up to my 40+ user preferences?

# WPF LinkLabel 4/18/2009 12:27 AM Pedro Silva's Blog
WPF doesn't have a built in LinkLabel control as you'd find in WinForms. But, I have found a couple of

# WPF LinkLabel | Coded Style 4/18/2009 1:21 AM Pingback/TrackBack
WPF LinkLabel | Coded Style

# WPF LinkLabel | Coded Style 4/18/2009 3:28 AM Pingback/TrackBack
WPF LinkLabel | Coded Style

# WPF LinkLabel | Coded Style 4/18/2009 5:33 AM Pingback/TrackBack
WPF LinkLabel | Coded Style

# WPF LinkLabel | Coded Style 4/18/2009 12:18 PM Pingback/TrackBack
WPF LinkLabel | Coded Style

# re: WPF: implementing a LinkLabel control with XAML and VB code 6/22/2010 8:10 AM DOT NET Development
the guidance provided must be really very helpful to the developers as the main aim is to have a linklabel in the very formatted coding style

Post Feedback

Title:
Name:
Url:
Comments: 
Codice di sicurezza
Protected by FormShield