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”&gt;

            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&#8221; 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 ….

 


11 thoughts on “Using Hyperlink in WPF application

  1. Good day! I know this is somewhat off topic but I was wondering if you
    knew where I could locate a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having difficulty finding one?
    Thanks a lot!

  2. I’m using the code below. I hope it can help you.

    Click here!

    private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
    {
    System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(e.Uri.AbsoluteUri));
    e.Handled = true;
    }

  3. Hi,

    Just one question, please….

    In the code line “Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));”… what stands for Process?? I´m trying to write your code and I got an error regarding that line…

    Thanks in advance

  4. Pingback: 2010 in review « Nishant Rana's Weblog

  5. This doesnt seem to work unless i add a this.DataContext = “something” into my main window class. this is causing issues for me as i have a relativesource binding set to my datacontext in the xaml. how can i get around this? thanks! great article btw

  6. Pingback: Using Hyperlink in WPF application « Nishant Rana’s Weblog « Nam Gi VU Blogs

  7. Hi,
    I am Naga Harish from chennai. I need one help from you. I want write Microsoft certification in ASP.Net. I need your guide lines. You can my gmail ID to send reply (nagaharish.movva@gmail.com) . I have some Q? too. Please help me!

  8. 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;
    }
    }

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s