How to create hyperlinks in silverlight(in windows presentation foundation)?
Following is the process to create hyperlinks in Silverlight;
- Select Canvas control in Toolbox in main XAML document and draw a canvas object on the artboard.
- Select Canvas control in Toolbox in main XAML document and draw a canvas object on the artboard.
- Open Properties panel and set the Cursor property to Hand under Common Properties.
- Double click on the renamed object and activate it. Around the canvas, a yellow bounding box can be observed for identifying it as the activated object.
- Select the TextBlock control, and draw a text block object inside the renamed object.
- Ensure to be in text editing mode. To do this press F2. The content of the text block need to be changed to Link. Press Esc to exit from text editing mode.
- Double the code-behind the file for XAML document under Files in the Project panel. In the Java Script editor, the code behind file opens inside the Expression Blend 2.
- A line of code for event handler already exists and is similar to the following
rootElement.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
Append the following code below the line: to respond to the MouseLeftButtonDown event.
if (null != this.control.content.findName("Renamed Object name here"))
this.control.content.findName("Renamed Object name here").addEventListener("MouseLeftButtonDown",
Silverlight.createDelegate(this, this.handleHyperlink));
- Append the following event handler method before handleMouseDown event:
handleHyperlink: function(sender, eventArgs)
{
window.location = http://www.somewebsitename.com;
}
This method will be invoked by clicking the left mouse button inside the canvas object and redirects the browser to the specified web site.
- Test your application by pressing F5.