8

I am extending the functionality of a WPF Richtextbox. I want certain text to become bold when I type it in. I was able to get certain text to bold but the text following the bolded word would also become bolded...

Heres a sample 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 what I get:

NOTBOLDED NOTBOLDED BOLDED NOTBOLDED

**Please note that it becomes bolded while typing.

How do I prevent the text after a bolded word from also becoming bolded?


Not duplicate question since the accepted solution for provided link is for WinForms and the rest are for preset text.

4

2 回答 2

3

经过几次测试,我想出了一个简单的解决方案。

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

这将插入符号设置在正确的方向,防止 BOLD 设置在 Run 对象中继续。

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

这会将 Run 对象添加到位于 Paragraph 对象末尾的新 Bold 对象的末尾。

于 2015-11-06T11:05:26.017 回答
1

您将需要检测何时不再检测到所需的文本,可能是否出现空格,然后删除粗体值并将其重置为正常值。

于 2015-11-06T04:47:42.880 回答