Share via


Try it: Create a hyperlink that opens a browser window

This page applies to WPF projects only

To create a hyperlink that opens an Internet browser window, you must use an event handler method. The following procedure shows you how to create a hyperlink in a text control that will open a browser window to a URL that you specify.

If you only want to create a hyperlink in a Page document that will move to another Page document in your application or to a web page, you can use the Hyperlink button in the Properties panel. For more information, see Create a hyperlink in a Page document.

  1. In Design view, draw a text control such as a Button or a Label on the artboard. For more information, see Add a text control to the artboard.

  2. Select the new text object under Objects and Timeline.

  3. In the Properties panel, click the Events button Cc304466.6c67bb3b-e8a2-4a63-bad5-54d5c15b04dd(en-us,Expression.10).png in the upper-right area of the Properties panel.

    The Properties panel switches to Events view.

  4. Locate one of the following events:

    • Click   This event exists for controls such as Button, CheckBox, and others.

    • MouseDown   This event exists for most other controls such as Label or Grid.

  5. Enter a name in the text box for the event, or double-click in the text box to create the initial code for the event handler method.

    If you have Microsoft Visual Studio 2008 installed, Microsoft Expression Blend generates the event handler method in your document's code-behind file and then opens the file in Visual Studio 2008. If you do not have Visual Studio 2008 installed, Expression Blend copies the event handler method code to the Clipboard, and then you must open the code-behind file in a text editor to paste in the code. For more information, see Edit a code-behind file.

  6. Paste the following code inside the event handler method:

    try
    {
      System.Diagnostics.Process.Start("https://www.microsoft.com");
    }
    catch {}
    
    Try
    System.Diagnostics.Process.Start("https://www.microsoft.com")
    Catch
      'Code to handle the error.
    End Try
    

    If you used a Click event, and named it OnClick, your event handler method should resemble the following:

    private void OnClick(object sender, RoutedEventArgs e)
    {
        try    {System.Diagnostics.Process.Start("https://www.microsoft.com");    }    catch {}
    }
    
    Private Sub OnClick(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        TrySystem.Diagnostics.Process.Start("https://www.microsoft.com")    Catch        'Code to handle the error.    End Try
    End Sub
    

    If you used a MouseDown event, and named it OnMouseDown, your event handler method should resemble the following:

    private void OnMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        try    {System.Diagnostics.Process.Start("https://www.microsoft.com");    }    catch { }
    }
    
    Private Sub OnMouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        TrySystem.Diagnostics.Process.Start("https://www.microsoft.com")    Catch        'Code to handle the error.    End Try
    End Sub
    
  7. Save your files, and then press F5 to run your application and test your hyperlink.

  1. Draw a TextBlock control on the artboard. For more information, see Add a text control to the artboard.

  2. Right-click the TextBlock under Objects and Timeline, and then click View XAML.

    The artboard switches to Split view with the line of XAML code for the TextBlock highlighted.

  3. Replace your TextBlock object with the following XAML code:

    <TextBlock TextWrapping="Wrap">
      <Run Text="Click "/>
      <Hyperlink Click="OnClick">
        <Run Text="here"/>
      </Hyperlink>
      <Run Text=" for more information"/>
    </TextBlock>
    

    In this XAML code, the TextBlock has child elements for the text before and after the link, and the hyperlink itself. You might have to reposition or resize your TextBlock element because this sample XAML code positions the TextBlock in the upper-left area of the artboard.

  4. If you want to format the content of your TextBlock element or add more text, select your TextBlock object in Design view, and then press F2 to enter text-editing mode. In text-editing mode, you can also add other content to your TextBlock, such as images, or other objects. For more information, see Edit text, Format text, and Add an object to text flow.

  5. To make the hyperlink work, you must add a Click event handler method. Open the code-behind file for your document by double-clicking the file in the Project panel. Expression Blend opens the file in Visual Studio 2008 if it is installed. If you do not have Visual Studio 2008 installed, Expression Blend opens the code-behind file in whichever text editor is associated with .cs or .vb file name extensions. If you do not have any program associated with those file name extensions, you will have to open a text editor and then open your code-behind file in the text editor. For more information, see Edit a code-behind file.

  6. In your code-behind file, paste the following code inside the class declaration (for example, before the End Class line in a .vb file, or before the second last } in a .cs file):

    private void OnClick(object sender, RoutedEventArgs e)
    {
        try    {System.Diagnostics.Process.Start("https://www.microsoft.com");    }    catch {}
    }
    
    Private Sub OnClick(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        TrySystem.Diagnostics.Process.Start("https://www.microsoft.com")    Catch        'Code to handle the error.    End Try
    End Sub
    
  7. Save your files, and then press F5 to run your application and test your hyperlink.

See also

Concepts

Create a hyperlink in a Page document

Create a hyperlink in a Silverlight application