Question

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!

Was it helpful?

Solution

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;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top