Lubo Blagoev's Blog

My thoughts on software and technology

Building a WPF LinkLabel control


Currently there is no LinkLabel control for WPF and that's why I decided to build one. First I will show what is currently available in WPF.

If you want to simulate LinkLabel you can use inline Hyperlink in a TextBlock.

 
<TextBlock>
    <Hyperlink>
        <Run Text="Test link"/>
    </Hyperlink>
</TextBlock>
 
You can use Label control and inline there a hyperlink but I think TextBlock is better since you can edit the properties of all the child elements from InlineCollection directly in Expression Blend UI.

 

  WPF LinkLabel 1

While this approach have benefits I don't like it much so I though why not write a better LinkLabel control similar to the one in Windows Forms. And so I did. First let me show you what the control looks like.

 A WPF LinkLabel

The first one is a default LinkLabel control, the second one have its  LinkLabelBehavior set to HoverUnderline and the third is using custom colors for Foreground and HoverForeground properties.

The LinkLabel control supports

  • Foreground and HoverForeground properties
    • Allows setting custom properties of both foreground and hoverforeground properties
  • LinkLabelBehavior property
    • Allows setting the underline behavior when cursor hovers or not the LinkLabel
  • Custom HyperlinkStyle property
    • You can use this property to set specific custom defined style for the internal hyperlink of the LinkLabel. If set override custom  foreground and hoverforeground colors
  • Url - the navigation target property.
  • All other properties are inherited from the standard System.Windows.Controls.Label control

Settings these properties is easy using Blend or xaml code.  

<ThemedControlLibrary:LinkLabel Content="Link Label" FontSize="22"/>
<ThemedControlLibrary:LinkLabel Content="Link Label" LinkLabelBehavour="HoverUnderline" />
<ThemedControlLibrary:LinkLabel Foreground="#FF847901" HoverForeground="#FF06C8F2" Content="Link Label" LinkLabelBehavour="NeverUnderline"/>

 

WPF LinkLabel

Here is how this control can be used in a window. Just add namespace to the xaml and use the control.

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="DemoApplication.Window1"
    Title="DemoApplication" Height="300" Width="300"
    xmlns:ThemedControlsLibrary="clr-namespace:ThemedControlsLibrary;assembly=ThemedControlsLibrary"
    >
    <Grid>
        <ThemedControlsLibrary:LinkLabel HorizontalAlignment="Left" VerticalAlignment="Top" Content="LinkLabel"/>
    </Grid>
</Window>

 

The whole code of the control as simple as it can be. It just defines the desired properties and the appropriate category attributes that control where these properties are shown in Blend Designer.

public class LinkLabel : Label
{
    private const string _linkLabel = "LinkLabel";

    public static readonly DependencyProperty UrlProperty = DependencyProperty.Register("Url", typeof(Uri), typeof(LinkLabel));

    [Category("Common Properties"), Bindable(true)]
    public Uri Url
    {
        get { return GetValue(UrlProperty) as Uri; }
        set { SetValue(UrlProperty, value); }
    }

    public static readonly DependencyProperty HyperlinkStyleProperty = DependencyProperty.Register("HyperlinkStyle", typeof(Style),
            typeof(LinkLabel));

    public Style HyperlinkStyle
    {
        get { return GetValue(HyperlinkStyleProperty) as Style; }
        set { SetValue(HyperlinkStyleProperty, value); }
    }

    public static readonly DependencyProperty HoverForegroundProperty = DependencyProperty.Register("HoverForeground", typeof(Brush),
            typeof(LinkLabel));

    [Category("Brushes"), Bindable(true)]
    public Brush HoverForeground
    {
        get { return GetValue(HoverForegroundProperty) as Brush; }
        set { SetValue(HoverForegroundProperty, value); }
    }

    public static readonly DependencyProperty LinkLabelBehavourProperty = DependencyProperty.Register("LinkLabelBehavour",
            typeof(LinkLabelBehaviour),
            typeof(LinkLabel));

    [Category("Common Properties"), Bindable(true)]
    public LinkLabelBehaviour LinkLabelBehavour
    {
        get { return (LinkLabelBehaviour)GetValue(LinkLabelBehavourProperty); }
        set { SetValue(LinkLabelBehavourProperty, value); }
    }

    static LinkLabel()
    {
        FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
            typeof(LinkLabel),
            new FrameworkPropertyMetadata(typeof(LinkLabel)));
    }
}

 

In order for the Content to be bindable I used the BindableRun implemented by Filipe Fortes here. I added this BindableRun object to the control template like so:

<local:BindableRun BoundText="{Binding RelativeSource= {RelativeSource TemplatedParent}, Path=Content}"/>	
 
In order to have theme support  for any control you should have a Themes folder in the project that contains the Styles of your control. The LinkLabel defines only one such file Generic.xaml since the hyperlink style is the same no matter what is the current system theme. I will not include the control template of the LinkLabel but it consist of a inline Hyperlink in a TextBlock just like you may have done it with out the LinkLabel control. Check it in the attached project.
That's all about it. Simple and beautiful. Download and use it.
 
[Update 25.11.2007] Fixed the RequestEvent and ClickEvent registration and firing.
[Update 10.11.2007] I have updated the control to include WPF commands support and fixed IsEnabled property behavior.
 

Download the source code Themed Controls Library.zip or just the compiled assembly ThemedControlsLibrary.dll

Currently rated 5.0 by 4 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

November 10, 2007 17:29 by lubo
Tags: , ,
Categories:

Comments

March 27. 2008 14:40 by blogs.windowsclient.net

Pingback from blogs.windowsclient.net

Lubo Blagoev: <LinkLabel /> for WPF - Rob Relyea - Xamlified

May 18. 2009 23:42 by Lotfi

What is the license for using it?

July 3. 2009 22:05 by aprogrammer.wordpress.com

Pingback from aprogrammer.wordpress.com

A simple LinkButton « A Programmer's Weblog

July 9. 2009 17:54 by lubo

The license for using it is Microsoft Permissive License (Ms-PL) as said in the Disclaimer.

October 20. 2009 12:15 by Hosting

I have implemented a WPF LinkLabel check it out here.

February 2. 2010 00:29 by Simon

Nice, but consider spellchecking your property names (i.e. LinkLabelBehavour)

February 2. 2010 03:17 by lubo

Thanks. Good to see that someone is paying attention.

March 4. 2010 05:29 by samir

we don't have to create the custom controls for link label. you can achieve the same functionality with triggers and TextDecorations.

Thanks and Regards,
Samir

March 4. 2010 20:00 by lubo

Hi Samir,
I agree that we don't need to create it. This doesn't mean we shouldn't. And I see some people are using it. Back in 2007 it seemed good practice. In general controls should provide behavior. Like ToggleButton is different from Button. And why we have both?

By the way the Run is bindable in .NET 4.0 so we don't need BindableRun any more. See here: blogs.msdn.com/.../bindable-run.aspx

Add comment


 

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]


Live preview

May 19. 2013 20:18 by