Posted by: Nishant Rana | March 26, 2009

Using Hyperlink in WPF application

To user hyperlink in WPF application we could do something like this

 

<TextBlock>

<Hyperlink NavigateUri=”http://www.google.co.in”>

            Click here

</Hyperlink>

</TextBlock>

 

However the NavigateUri works only if we are placing the hyperlink within a page. To use it within a windows-based application we need to hanlde the RequestNavigate event and write the code ourselves.

 

Something like this

 

<TextBlock>           

<Hyperlink NavigateUri=”http://www.google.co.in” RequestNavigate=”Hyperlink_RequestNavigate”>

 Click here

</Hyperlink>            

</TextBlock>

 

private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)

  {

            Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));

            e.Handled = true;

  }

 

While using hyperlink we could also provide handler for Application.NavigationFailed event in case if navigation fails.

 

That’s it ….

 


Responses

  1. Very helpful. Just want to add that

    Application.NavigationFailed EventHandler goes into the App.Xaml class and might look like this:

    private void App_NavigationFailed (object sender, NavigationFailedEventArgs e)
    {
    if (e.Exception is System.Net.WebException)
    {
    //code to handle error here, such as a messagebox to the user

    e.Handled = true;
    }
    }


Leave a response

Your response:

Categories