How can I add a hyperlink to a selection in a wpf RichTextBox?
For example, if my RichTextBox says:
"Send me to Stack Overflow."
and I highlight "Overflow", then click a button to make a hyperlink out of it to go to "www.stackoverflow.com" How can I achieve this?
Also, I'm trying to highlight text from code, and I can't figure out how. To highlight the whole RichTextBox I have tried this, but it hasn't worked:
TextPointer myTextPointer1 = rtb.Document.ContentStart;
TextPointer myTextPointer2 = rtb.Document.ContentEnd;
rtb.Selection.Select(myTextPointer1, myTextPointer2);
TextSelection ts = rtb.Selection;
TextPointer start = ts.Start;
TextPointer end = ts.End;
TextRange before = new TextRange(rtb.Document.ContentStart, start);
TextRange after = new TextRange(end, rtb.Document.ContentEnd);
TextRange linker = new TextRange(start, end);
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Clear();
myParagraph.Inlines.Add(before.Text);
Hyperlink hyperLink = new Hyperlink();
hyperLink.Inlines.Add(ts.Text);
hyperLink.Click += new RoutedEventHandler(hyperLink_Click);
myParagraph.Inlines.Add(hyperLink);
myParagraph.Inlines.Add(after.Text);
rtb.Document.Blocks.Clear();
rtb.Document.Blocks.Add(myParagraph);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.