Paulo
Paulo

Reputation: 619

RTF with Links in a RichTextBox WPF

I am able to load an rtf document in a RichTextBox, but the links that the document contains to some websites are not working. Anyone have any idea why? Some solution to make the links work?

Best regards,
Paulo Azevedo

Upvotes: 2

Views: 3210

Answers (1)

Drew Marsh
Drew Marsh

Reputation: 33379

WPF by default doesn't understand where you want the links to be displayed, so what's happening is that the Hyperlink class is firing an event, RequestNavigate, and expecting you, the application designer, to cause the actual navigation to occur.

I assume you just want to launch the system configured web browser, so here's all you need to do:

  1. Hook the Hyperlink::RequestNavigate routed event
  2. Call Process.Start with the URL you receive to have the OS launch the browser.

That might look a little something like this:

public class MyWindow : Window
{
    public MyWindow()
    {
        this.InitializeComponent();

        this.myRichTextBox.AddHandler(Hyperlink.RequestNavigate, MyWidow.HandleRequestNavigate);
    }

    private static void HandleRequestNavigate(object sender, RequestNavigateEventArgs args)
    {
            Process.Start(args.Uri.ToString());
    }
}

Upvotes: 3

Related Questions