Attila Szász
Attila Szász

Reputation: 747

How to catch hyperlink clicks in UWP RichEditBox?

I'm trying to use RichEditBox in a UWP custom component, which would be added to a WPF application through XamlIslands:

<RichEditBox x:Name="editor" PointerPressed="editor_PointerPressed" Tapped="editor_Tapped" PointerReleased="editor_PointerPressed">

I add hyperlinks with the following way:

editor.Document.Selection.Link = "\"[the link]\"";

It works fine and it opens the link in the browser when Ctrl+Click on it, but how can I catch that click event?

None of the callbacks are fire which I defined as a parameter in RichEditBox, so no PointerPressed, no PointerReleased, and no Tapped events are fired at all.

Upvotes: 1

Views: 247

Answers (1)

Joshua Rogers
Joshua Rogers

Reputation: 3548

I managed to do it like this:

   public class CustomRichTextBox: RichEditBox
{
    protected override void OnTapped(TappedRoutedEventArgs e)
    {
        base.OnTapped(e);

        var tappedPoint = e.GetPosition(this);
        var textRange = Document.GetRangeFromPoint(tappedPoint, PointOptions.ClientCoordinates);
        textRange.StartOf(TextRangeUnit.Link, true);

        var mylink = textRange.Link;
    }
}

Upvotes: 4

Related Questions