Silverlight Tip of the Day #62 – How to Create a Hyperlink
Hyperlinks are a graphic or string that connects readers to another web site when clicked. Generally these are a few words that are identified by a blue color and a underline. Here is an example of a hyperlink:
Click Me
In Silverlight Hyperlinks can be created through the HyperlinkButton control.
Here is an example of a how to declare a HyperlinkButton control in XAML:
<HyperlinkButton Content="Click Me" NavigateUri="http://www.silverlight.net">
</HyperlinkButton>
Couple notes:
- Content is what is displayed to the user.
- NavigateUri is the destination the user is taken too when the link is clicked.
When run you will see this:
You can target the link to open the link in a new page or the same page by setting the TargetName property.
- TargetName = _blank, _media, _search = Open the link in a new window
- TargetName = _parent, _self, _top, “” = Open the link in the window the link was clicked.
Example opening a new page:
<HyperlinkButton Content="Click Me" TargetName="_blank" NavigateUri="http://www.silverlight.net">
</HyperlinkButton>
Opening the same page:
<HyperlinkButton Content="Click Me" TargetName="_self" NavigateUri="http://www.silverlight.net">
</HyperlinkButton> Additionally, the Hyperlink does not have to be represented by text. You can use any control such as a Image, etc. by setting the HyperlinkButton.Content. For example:
<HyperlinkButton NavigateUri="http://www.silverlight.net">
<HyperlinkButton.Content>
<Canvas>
<Rectangle Width="100" Height="100" Fill="Black" Stroke="Blue" StrokeThickness="2" ></Rectangle>
<TextBlock Canvas.Top="40" Canvas.Left="25">Click Me</TextBlock>
</Canvas>
</HyperlinkButton.Content>
</HyperlinkButton>
As shown when run:
Thank you,
--Mike Snow
Subscribe in a reader