WPF RichTextBox - 键入文本的格式

发布于 2024-08-27 21:10:35 字数 683 浏览 4 评论 0原文

我正在对 WPF RichTextBox 中的选定标记应用格式设置。为此,我得到一个包含我想要突出显示的标记的 TextRange。然后,我将像这样更改文本的颜色:

// Get start and end pointer for token
TextPointer startPointer = run.ContentStart.GetPositionAtOffset(startOffset);
TextPointer endPointer = run.ContentStart.GetPositionAtOffset(endOffset);

// Get text range for token
TextRange textRange = new TextRange(startPointer, endPointer);

// Highlight token
textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);

这发生在 RichTextBox 的 TextChanged 事件上。

格式按预期应用,但继续键入文本将导致新文本继承已应用于相邻单词的格式。我希望任何新文本的格式设置都使用 RichTextBox 属性中定义的默认格式设置选项。这可能吗?

或者,我可以使用默认格式选项突出显示我不希望显示为蓝色的所有标记,但这对我来说很尴尬。

I am applying formatting to selected tokens in a WPF RichTextBox. To do this I get a TextRange that encompasses the token that I would like to highlight. I will then change the color of the text like this:

// Get start and end pointer for token
TextPointer startPointer = run.ContentStart.GetPositionAtOffset(startOffset);
TextPointer endPointer = run.ContentStart.GetPositionAtOffset(endOffset);

// Get text range for token
TextRange textRange = new TextRange(startPointer, endPointer);

// Highlight token
textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);

This is happening on the TextChanged event of my RichTextBox.

The formatting is applied as expected, but continuing to type text will result in the new text inheriting the formatting that has already been applied to the adjacent word. I would like the formatting of any new text to use the default formatting options defined in the RichTextBox properties. Is this possible?

Alternatively I could highlight all tokens that I don't want be blue with the default formatting options but this feels awkward to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉殇 2024-09-03 21:10:35

这不是标准行为吗?如果我在 Word 中突出显示文本,将其设置为粗体,将光标移动到该文本旁边并键入,新文本也会变为粗体。我想微软让富文本框的工作方式与Word中的文本区域类似。

如果您根据匹配某些条件(例如语法突出显示)来选择标记,您可以尝试将样式应用于光标距标记 1 个字符之后的文本吗?例如:

一些令牌 |

而不是

一些令牌|

其中 | 是光标。在第一个示例中,光标与标记之间用空格分隔,因此,如果您的程序找到 SomeToken ,它将获取从 S 之前到 < 之前的文本范围。代码>(空格)。我想该样式不会应用于新输入的文本。

我承认这只是一个猜测,我可能从你的问题描述中误解了你的确切情况。更多代码可能会有所帮助,特别是您提到的 TextChanged 事件,或创建文本范围的代码。

Isn't that the standard behavior? If I highlight text in Word, make it bold, move the cursor next to that text and type, the new text becomes bold too. I'd imagine that MS made the rich text box work similarly to the text area in Word.

If you're selecting a token based on matching some criteria, like for syntax highlighting, could you try applying the style to the text after the cursor is 1 character away from the token? For example:

SomeToken |

instead of

SomeToken|

where | is the cursor. The cursor is separated from the token by a space in the first example, so if your program finds SomeToken it'll get the text range from before the S to before the (space). I'd imagine that the style won't be applied to newly entered text then.

I'll admit this is just a guess, and I might have misunderstood your exact situation from your problem description. Some more code might be helpful, particularly the TextChanged event you mention, or the code that creates the text range.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文