2

Maybe this is a stupid question, but how can I click (and capture the click event of) a link in a Windows 8 RichEditBox.

I've placed the link using RichEditBox.Document.GetRange(0, 10).Link = "\"foobar\"". The link itself is shown in the RichEditBox, but I can't click it.

Thanks for advices.

Anobik
  • 4,841
  • 2
  • 17
  • 32
Mrks83
  • 149
  • 10

2 Answers2

6

Here is a helper to add a link clicked event to the RichEditBox:

public class LinkClickedEventArgs
{
    public string LinkText { get; set; }
}

public class RichEditBoxWithHyperlink :RichEditBox
{

    public event EventHandler<LinkClickedEventArgs> LinkClicked;
    protected override void OnTapped(TappedRoutedEventArgs e)
    {
        base.OnTapped(e);
        if (LinkClicked != null)
        {
            Point tappedPoint = e.GetPosition(this);
            ITextRange textRange = this.Document.GetRangeFromPoint(tappedPoint, PointOptions.ClientCoordinates);
            textRange.StartOf(TextRangeUnit.Link,true);

            if (!string.IsNullOrEmpty(textRange.Link))
            {
                LinkClicked(this, new LinkClickedEventArgs(){LinkText = textRange.Link});
            }
        }
    }
}
Benoit Catherinet
  • 3,335
  • 1
  • 13
  • 12
  • The Tapped event does not fire for me when the control is not enabled, and i *think* readonly. I ended up making a transparent control over it, and also using screen coordinates (GetPosition(null) and PointOptions.None) because they didn't seem to line up correctly for me. I was on windows phone 8.1 though, and I believe there's some functional differences to this control - it's converted to Xaml on desktop I've heard, but true Rtf on phone. – RichTea Dec 29 '14 at 10:30
0

RichEditBox lacks WPF's RichTextBox's LinkClicked event. There is no way to detect whether the link was clicked or not. You can open the hyperlink only by pressing ctrl and clicking on link.

How can I make a hyperlink work in a RichTextBox? - This what in WPF

Community
  • 1
  • 1
Farhan Ghumra
  • 15,180
  • 6
  • 50
  • 115