views:

309

answers:

1

Hello all,

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

+1  A: 

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());
    }
}
Drew Marsh
Hello Drew,Thanks for your answer. The problem is not how you described it. The problem is that I have a word "Link" and this word has an hyperlink to an Website in the RTF document. In the RichTextBox the word appear with an underline, and the mouse over cursor is an hand, but the click does't work. Any clue?
Paulo
Ah, no problem. I know what you need, let me revise my answer.
Drew Marsh