(X) Hide this
    • Login
    • Join
      • Say No Bots Generate New Image
        By clicking 'Register' you accept the terms of use .

Using the HyperlinkButton control in Silverlight

(3 votes)
Nikolay Raychev
>
Nikolay Raychev
Joined Mar 28, 2008
Articles:   22
Comments:   58
More Articles
11 comments   /   posted on May 27, 2008
Categories:   Controls

This article is compatible with the latest version of Silverlight.

Introduction

The HyperlinkButton is a Silverlight control which acts as a hyperlink.

See also:
Button Controls Article
Button Article
ToggleButton Article
RepeatButton Article

Overview

Here is a little example showing how to use the HyperlinkButton:

XAML:

<UserControl x:Class="HyperlinkButton2.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="300" Height="100">
    <Canvas x:Name="LayoutRoot" Background="White">
        <HyperlinkButton x:Name="hbtnTest" TargetName="_blank" Content="SilverlightShow" NavigateUri="http://www.silverlightshow.net" Canvas.Top="40" Canvas.Left="30"></HyperlinkButton>
    </Canvas>
</UserControl>

The most important property of the HyperlinkButton is the NavigateUri. It gets or sets the URI to navigate when the button is clicked. It can be an absolute or a relative URI.

Another useful property is the TargetName. It determines the target window or frame where the URI, specified by NavigateUri will be opened. In our example it will open in a new window. For a list of available values visit:

http://msdn.microsoft.com/en-us/library/system.windows.controls.hyperlinkbutton.targetname(VS.95).aspx

If you want to see how you can attach to the Click event or use ClickMode property visit the Button Controls Article. These members are inherited from the ButtonBase class. The example there is with a Button but it is the same with a HyperlinkButton.

A cool new feature in the final release is that when you disable your HyperlinkButton (set the IsEnabled property to false) the cursor chnges from Hand (the default one for the HyperlinkButton) to Arrow. It's quite intuitive because when you are not able to click the Hand cursor could make an illusion that when you click somthing will happen. This feature didn't exist till the official release.

Conclusion

This article is just a brief description of the key features of the HyperlinkButton control. It targets the developer who has just started with the Silverlight controls. Any comments are welcome.

Reference

http://msdn.microsoft.com/en-us/library/system.windows.controls.hyperlinkbutton(VS.95).aspx


Subscribe

Comments

  • -_-

    RE: Using the HyperlinkButton control in Silverlight 2


    posted by Delay on May 27, 2008 15:56

    Note: This comment was related to the Silverlight 2 Beta 1 release

    Nikolay,

    The reason you can't see the control is that Silverlight 2 Beta 1 by default does not apply default styles unless the class *exactly* matches the TargetType in the definition of the default style in generic.xaml. So, in your case MyHyperlinkButton is not HyperlinkButton and you don't get the expected style/template. You probably want to either explicitly specify the style/template in the XAML (mainly for quick testing) or else add a generic.xaml to your own assembly with a copy of the default HyperlinkButton style/template that specifies TargetType=MyHyperlinkButton for both the Style and the Template.

    Here's a tool to make part of this a bit easier: http://blogs.msdn.com/delay/archive/2008/03/22/improving-everyone-s-access-to-silverlight-2-s-generic-xaml-resources-silverlightdefaultstylebrowser-tool-and-source-code.aspx

    PS - Thank Silverlight Cream/Dave Campbell for pointing me here. :)

  • nikolayraychev

    RE: Using the HyperlinkButton control in Silverlight 2


    posted by nikolayraychev on May 29, 2008 00:47

    Note: This comment was related to the Silverlight 2 Beta 1 release

    Thanks for your reply, Delay.

    Your tool looks really cool J

    Obviously extending a Silverlight control is not as easy as extending the class as we were used to in ASP.NET.  I saw your article and tried adding the generc.xaml file to my Silverlight Class Library project but this did not help.

    The XAML is exactly the same like the HyperlinkButton template. The only change I made was that I added the ResourceDictionary root element which I saw from here: http://www.silverlight.net/Quickstarts/BuildUi/CustomControl.aspx and changed the target names. But this did not help.

  • -_-

    RE: Using the HyperlinkButton control in Silverlight 2


    posted by Sudhir Malireddy on Jul 29, 2008 01:58

    Note: This comment was related to the Silverlight 2 Beta 2 release

    this does't work with silverlight 2.0 beta 2

    can u plz give solution for this

  • nikolayraychev

    RE: Using the HyperlinkButton control in Silverlight 2


    posted by nikolayraychev on Aug 04, 2008 01:50

    Note: This comment was related to the Silverlight 2 Beta 2 release

    Hi Sudhir,

    my example in the "Issues" section of this article works in Silverlight 2 Beta 2. When extending the HyperlinkButton the new class takes the default style from its base class.

  • -_-

    RE: Using the HyperlinkButton control in Silverlight 2


    posted by mfc2wpf on Nov 25, 2008 10:48

    Why isn't there a target for "Open in a new tab?"

  • -_-

    RE: Using the HyperlinkButton control in Silverlight 2


    posted by Padawan on Dec 19, 2008 23:19

    @ mfc2wpf

    The "new tab" is simply   _blank  (like, target a blank page).   Check the link:  http://msdn.microsoft.com/en-us/library/system.windows.controls.hyperlinkbutton.targetname(VS.95).aspx

     _self   is used for the current window.  I'm not sure the uses for other target names.

  • -_-

    RE: Using the HyperlinkButton control in Silverlight 2


    posted by rene___ on Jan 21, 2009 07:34

     Is there a way to get a reference to the opened window object?

  • Enrai

    RE: Using the HyperlinkButton control in Silverlight 2


    posted by Enrai on Jan 27, 2009 08:33

    Hi rene___,

    I couldn't find a way to get that reference, which doesn't mean that there isn't one, but I am more likely thinking that this is not possible for now.

  • -_-

    RE: Using the HyperlinkButton control in Silverlight 2


    posted by John Public on Jul 03, 2009 21:45

    Hi--I found that for Internet Explorer the default permissions do not allow "_blank" (new window).  But current window "_parent" works fine, i.e.:

    <HyperlinkButton x:Name="hbtnTest" TargetName="_parent" Content="CNN" NavigateUri="http://www.cnn.com"></HyperlinkButton>

  • nikolayraychev

    RE: Using the HyperlinkButton control in Silverlight 2


    posted by nikolayraychev on Jul 06, 2009 10:34

    Hi John,

    I don't have such problems in IE. My _blank hyperlink works fine :)

  • -_-

    RE: Using the HyperlinkButton control in Silverlight 2


    posted by Selvan on Feb 08, 2010 13:16
    I am having a silverlight Datagrid. It has 5 columns and one column contains Hyperlink button, When i click on the Hyperlink button, I need to Fetch the complete row information and pass on to another page. How to implement it?

Add Comment

Login to comment:
  *      *