C# Hyperlink in TextBlock: nothing happens when I click on it
Asked Answered
F

7

27

In my C# standalone application, I want to let users click on a link that would launch their favorite browser.

System.Windows.Controls.TextBlock text = new TextBlock();
Run run = new Run("Link Text");

Hyperlink link = new Hyperlink(run);
link.NavigateUri = new Uri("http://w3.org");
text.Inlines.Add(link);

The link is displayed correctly.

When I move the mouse over it, the link becomes red.

PROBLEM: When I click it, nothing happens.

Did I forget something? Do I need to implement some kind of method to really let the link be opened?

Flunk answered 5/10, 2012 at 8:48 Comment(0)
T
35

You need to handle the hyperlink's RequestNavigate event. Here's a quick way of doing it:

link.RequestNavigate += (sender, e) =>
{
    System.Diagnostics.Process.Start(e.Uri.ToString());
};
Talishatalisman answered 5/10, 2012 at 8:59 Comment(0)
S
7

Are you handling the 'Hyperlink.RequestNavigate' event? When a user clicks a Hyperlink in a WPF window it doesn't automatically open a browser with the URI specified in its NavigateUri property.

In your code-behind you can do something like:

link.RequestNavigate += LinkOnRequestNavigate;

private void LinkOnRequestNavigate(object sender, RequestNavigateEventArgs e)
{
    System.Diagnostics.Process.Start(e.Uri.ToString());
}
Silures answered 5/10, 2012 at 9:1 Comment(1)
WPF Hyperlink is nearly useless. I want it to launch the URL using url set in xaml, otherwise why don't I just use a label and button with my view viewmodel.Rill
R
3

You can make a global hyperlink handler in your App.xaml.cs

protected override void OnStartup(StartupEventArgs e) {
    EventManager.RegisterClassHandler(
        typeof(System.Windows.Documents.Hyperlink),
        System.Windows.Documents.Hyperlink.RequestNavigateEvent,
        new System.Windows.Navigation.RequestNavigateEventHandler(
            (sender, en) => Process.Start(new ProcessStartInfo(
                en.Uri.ToString()
            ) { UseShellExecute = true })
        )
    );
    base.OnStartup(e);
}

This assumes all the NavigateUri properties refer to something you want to launch, but you can always make the handler take care of edge cases.

Reverential answered 4/3, 2021 at 15:19 Comment(3)
This doesn't work in .NET 6.0Fry
Actually it was 5 that broke it, it needs UseShellExecute = trueReverential
Actually it should be en.Uri.ToString() instead of e.Uri.ToString().Fry
E
2

For those in .Net Core, the way you do this has changed. Based on this answer and this.

link.RequestNavigate += (sender, e) =>
{
    var url = e.Uri.ToString();
    Process.Start(new ProcessStartInfo(url)
    { 
        UseShellExecute = true 
    });
};
Eparchy answered 16/4, 2021 at 21:57 Comment(0)
A
1

Working with a command is also possible:

<TextBlock>
    See our <Hyperlink NavigateUri="https://www.example.com" Command="{Binding OpenPrivacyPolicyCommand}">Privacy Policy</Hyperlink>
</TextBlock>

The command needs to call open the URL then:

Process.Start(url);
Altocumulus answered 13/5, 2022 at 9:47 Comment(0)
G
0

When using MVVM

In your .xaml

<TextBlock>
  <Run Text="Gold buyers " /><Hyperlink Command="{Binding ViewGoldBuyersGuideCommand}" ><Run Text="guide" /></Hyperlink>
</TextBlock>

And in your vm.cs file

public ICommand ViewGoldBuyersGuideCommand { get; private set; }

ViewGoldBuyersGuideCommand = new DelegateCommand<object>(OnViewGoldBuyersGuide); // inside your constructor

private void OnViewGoldBuyersGuide(object obj)
{
    Process.Start("https://example.com/");
}
Giese answered 22/1 at 5:45 Comment(0)
C
0

On MVVM:

<TextBlock>         
    <Hyperlink Command="{Binding OpenHyperlinkCommand}" CommandParameter="https://www.google.com/">Google</Hyperlink>
</TextBlock>
ICommand OpenHyperlinkCommand {get;}

OpenHyperlinkCommand = new RelayCommand(url => Process.Start(url as string));
Can answered 22/2 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.