0

I am adding a link to my WPF app using Hyperlink in a TextBox:

<TextBlock Margin="480,92,460,713" Height="24">
 <Hyperlink NavigateUri="{Binding MyLink}" RequestNavigate="Hyperlink_RequestNavigate">My Link</Hyperlink>
</TextBlock>

The Binding "MyLink" does not work. The link I need to use has a query string with a variable i need to change dynamically in the code. If I try to even hardcode the link into the XAML i get an error because the query string has a variable with an ampersand.

My link is working when i point it to a site like google. but i need to set it in the c# code and be able to set my variable in the query string. is there a way to do this? thanks!

Drew
  • 2,601
  • 6
  • 42
  • 65

1 Answers1

1

What you are doing should work...

To test this create a default WPF application and place the below code within the Grid of Window1.xaml...

        <TextBlock>
             <Hyperlink NavigateUri="{Binding}" RequestNavigate="Hyperlink_RequestNavigate">My Link</Hyperlink>
        </TextBlock>

...in Window1.xaml.cs add this...

    public Window1()
    {
        InitializeComponent();

        this.DataContext = "whatever the heck i want";
    }

    private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
    {
        //e.Uri will display "whatever the heck i want" 
        //which would allow you to do whatever you want 
        //with the URL at that point

        Process.Start(new ProcessStartInfo("url_you_want_to_use"));
        e.Handled = true;
    }
Aaron McIver
  • 24,527
  • 5
  • 59
  • 88
  • still not working for me for some reason. when i changed the datacontext string to be a link, such as to google i click the link and nothing happens. – Drew Dec 29 '10 at 19:54
  • The code above is not going to do anything with the link; you need to handle that yourself...modified example...swap out "url_you_want_to_use" with e.Uri.AbsoluteUri to see it work immediately with http://www.google.com for instance – Aaron McIver Dec 29 '10 at 20:34