且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何突出显示而不是选择数据绑定的WPF TextBox或RichTextBox中的文本?

更新时间:2022-03-17 06:52:31

     string example = "example";
     string exampleDoc = "hello, example, hello example.";
     FlowDocument doc =
        new FlowDocument(new Paragraph(new Run("hello, example, hello example.")));
     int pos = 0;
     while (0 <= (pos = exampleDoc.IndexOf(example)))
     {
        new Bold(doc.ContentStart.GetPositionAtOffset(pos),
                 doc.ContentStart.GetPositionAtOffset(pos + example.Length));
     }

这将使您入门.通过将要突出显示的单词的ConverterParameter将其放在从字符串到FlowDocument的ValueConverter中是很容易的.当您添加更多格式时,请注意TextPointer的更改,您将看到突出显示向左移动.我相信您会找出适合您情况的***方法.

This will get you started. It would be easy to put this in a ValueConverter from string to FlowDocument with a ConverterParameter of the word you want to highlight. Watch out for the TextPointer changing as you add more formats, you'll see the highlighting shift to the left. I'm sure you'll figure out the best way to handle this in your situation.