Set selected text to bold in WPF RichTextBox

I am expanding the functionality of WPF Richtextbox. I want a certain text to become bold when I enter it. I was able to get the selected text in bold, but the text following the bold word will also become bold ...

Here is an example of my code:

private bool _Running = false;
void CustomRichTextBox_TextChange(object sender, TextChangedEventArgs e)
{
    if(_Running)
        return;
    _Running = true;

    //Logic to see if text detected

    //Logic to get TextPointers

    //Logic to get TextRange
    var boldMe = new TextRange(textPointer1, textPointer2);
    //Bold text
    boldMe.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);

    _Running = false;
}

I want:

NOTBOLDED NOTBOLDED BOLDED NOTBOLDED

but I get:

NOTBOLDED NOTBOLDED BOLDED NOTBOLDED

** Please note that it is printed in bold when printing.

How to prevent text selection after the shifted word?


This is not a repeating question, because the decision made for the provided link is intended for WinForms, and the rest is for predefined text.

+4
2

.

CaretPosition = CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward);

, BOLD Run.

if(textPointerEnd.GetNextInsertionPosition(LogicalDirection.Forward) == null)
    new Run("", textPointerEnd);

Run , Paragraph.

+2

, , , , reset .

+1

Source: https://habr.com/ru/post/1614677/


All Articles