0

我遇到了另一个问题RichTextBox。当您有一个像这样颜色的文本范围时:

var range = new TextRange(TextInput.Document.ContentStart,
TextInput.Document.ContentEnd);
range.Text = "asdfghjk";

var colored = new TextRange(TextInput.Document.ContentStart.GetPositionAtOffset(3, LogicalDirection.Forward),
TextInput.Document.ContentStart.GetPositionAtOffset(7, LogicalDirection.Forward));
colored.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);

在您的更改侦听器中,您有以下内容:

var range = new TextRange(TextInput.Document.ContentStart, TextInput.Document.ContentEnd);
if (range.Text.Length == 9)
     range.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);

当使用退格键删除其中一个蓝色的字符时(我认为删除键工作正常),并且您立即开始输入,出现的文本是 blue,就像格式化之前一样。我对为什么会发生这种情况以及避免它的方法很感兴趣。

4

0 回答 0