none
Hyperlinks in WPF RichTextBox

    Question

  • I was developing a little application derived from a WinForms RTB that incorporated "pseudo-hyperlinks". These look like links, but can't be deleted as text can. They have a text field that shows in the RTB, a tag, and other properties. When they are double-clicked, they open an editor. The app. maintains a list of these objects. I used the RTF \protect & \v flags, to mark a piece of text and a hidden tag. I detected a click on the hyperlink, found the tag, and used that to look it up in the list for purposes of editting or deletion.

    I am trying to replicate similar behavior with a WPF RTB. I tried the following:

    private void Temp()

    {

    RichTextBox control = new RichTextBox();

    control.IsDocumentEnabled = true;

    FlowDocument fd = new FlowDocument();

    Paragraph para = new Paragraph();

    para.Inlines.Add("First line. ");

    Run r = new Run("Text for hyperlink. ");

    Hyperlink h = new Hyperlink(r);

    h.Click += new RoutedEventHandler(h_Click);

    para.Inlines.Add(h);

    para.Inlines.Add("Last line.");

    fd.Blocks.Add(para);

    }

    void h_Click(object sender, RoutedEventArgs e)

    {

    TextRange documentTextRange = new TextRange(mRtb.Document.ContentStart, mRtb.Document.ContentEnd);

    }

    This works fine except for the following:
    1) If I move the hyperlink in the RTB, it still looks like a link (and the cursor changes), but the click event isn't trapped any more.
    2) I can delete it along with text - I want to be able to protect it from easy deletion.
    3) I would rather use double click instead of control+click.
    Can anyone suggest how I can do any of this, or how I can access the RTF as in WinForms?
    Thursday, August 07, 2008 12:23 AM

Answers

  • -> 1) If I move the hyperlink in the RTB, it still looks like a link (and the cursor changes), but the click event isn't trapped any more.

    Hyperlink only kicks in when you are hosted inside NavigationWindow or Frame which supports native navigation, for other scenario, please hook up to Hyperlink.MouseLeftButtonDown to implement your own navigation logic.

    -> 2) I can delete it along with text - I want to be able to protect it from easy deletion.

    As far as I know, WPF's RichTextBox doesn't support this type of scenario, you could send the feature request on this at the connect site:
    https://connect.microsoft.com/feedback/default.aspx?SiteID=212&wa=wsignin1.0

    ->3) I would rather use double click instead of control+click.
    You could hook up to Hyperlink.MouseLeftButtonDown event, and inside the event handler, you could write something like the following:

    private void hyperlink_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ClickCount  == 2)
        {
            //Handle mouse double click event here.
        }
    }

    Hope this helps
    • Marked as answer by Marco Zhou Wednesday, August 13, 2008 9:40 AM
    Monday, August 11, 2008 8:06 AM

All replies

  • -> 1) If I move the hyperlink in the RTB, it still looks like a link (and the cursor changes), but the click event isn't trapped any more.

    Hyperlink only kicks in when you are hosted inside NavigationWindow or Frame which supports native navigation, for other scenario, please hook up to Hyperlink.MouseLeftButtonDown to implement your own navigation logic.

    -> 2) I can delete it along with text - I want to be able to protect it from easy deletion.

    As far as I know, WPF's RichTextBox doesn't support this type of scenario, you could send the feature request on this at the connect site:
    https://connect.microsoft.com/feedback/default.aspx?SiteID=212&wa=wsignin1.0

    ->3) I would rather use double click instead of control+click.
    You could hook up to Hyperlink.MouseLeftButtonDown event, and inside the event handler, you could write something like the following:

    private void hyperlink_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ClickCount  == 2)
        {
            //Handle mouse double click event here.
        }
    }

    Hope this helps
    • Marked as answer by Marco Zhou Wednesday, August 13, 2008 9:40 AM
    Monday, August 11, 2008 8:06 AM
  • I wrote a little explanation of how to use RichTextBox and make links works that might help others who come across this question and don't exactly get what they were looking for as I didn't.

    Loading a RichTextBox from an RTF file using binding or a RichTextFile control
    http://www.rhyous.com
    Tuesday, August 02, 2011 4:29 AM