Determine if selected text is Hyperlink in RichTex

2019-06-13 16:24发布

问题:

I also am hoping to populate a text box with the URL of the selected Hyperlink. I think I am along the right path with this code, but I don't know how to complete it:

        TextPointer position = RichTextBoxEditor.Selection.Start;

        Inline parent = position.Parent as Inline;

        foreach (Hyperlink hl in RichTextBoxEditor.Blocks.OfType<Hyperlink>())
        {

        }

回答1:

Yes... you are in the right path. Never done before but if your cursor is inside an hyperlink this gives you the hyperlink:

TextPointer position1 = richTextBox1.Selection.Start;
Inline parent = position1.Parent as Inline;
TextPointer position2 = parent.ElementStart;

Hyperlink hl = position2.Parent as Hyperlink;