簡體   English   中英

在wpf RichTextBox中選擇文本的超鏈接並從代碼中設置選擇

[英]hyperlink selected text in wpf RichTextBox & set selection from code

如何在wpf RichTextBox中向選擇添加超鏈接?

例如,如果我的RichTextBox說:

“發送我到堆棧溢出。”

然后突出顯示“溢出”,然后單擊一個按鈕以使其超鏈接以轉到“ www.stackoverflow.com”。如何實現此目的?

另外,我試圖突出顯示代碼中的文本,但我不知道該怎么做。 為了突出顯示整個RichTextBox,我已經嘗試過了,但是沒有用:

    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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM