Question

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

Was it helpful?

Solution

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());
    }
}

OTHER TIPS

Here is one way that I solved links in rich text files.

Loading a RichTextBox from an RTF file using binding or a RichTextFile control

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top