Silverlight tip: Add an hyperlink

You can create Hyperlinks in Silverlight by using the HyperlinkButton control.

Here is a usage example in XAML:

<HyperlinkButton TargetName="_blank" Content=".Net Brainwork Hyperlink example" NavigateUri="http://blog.vascooliveira.com"></HyperlinkButton>

Some notes in this example:

  1. The TargetName property sets the name of the target window or frame that the Web page should open in, or the name of the object within the Silverlight application to navigate to. Possible values are:
    • _blank, _media, or _search: Loads the linked document into a new blank window.
    • _parent, _self, _top, or “”: Loads the page into the window in which the link was clicked (the active window).
  2. The NavigateUri is the link to navigate to.
  3. Content is the actual link text.

You can then, as usual, style the control to best fit your needs. See the following base style and start from there:

<Style x:Key="Hyperlink" TargetType="HyperlinkButton">
   <Setter Property="Padding" Value="2,0,2,0"/>
   <Setter Property="Cursor" Value="Hand"/>
   <Setter Property="HorizontalContentAlignment" Value="Left"/>
   <Setter Property="VerticalContentAlignment" Value="Top"/>
   <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="HyperlinkButton">
      <Grid Cursor="{TemplateBinding Cursor}">
       <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="Center" Margin="{TemplateBinding Padding}" VerticalAlignment="Center" ContentTemplate="{TemplateBinding ContentTemplate}"/>
      </Grid>
     </ControlTemplate>
    </Setter.Value>
   </Setter>
  </Style>

Technorati Tags:

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

0 comments so far

Add a comment