You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UWP技术问询:如何修改RichEditBox文档的全部字体颜色?

How to Change the Entire Document's Font Color in RichEditBox

Since you already have code working for modifying selected text's font color, scaling that to the whole document is super straightforward. Here are a couple of reliable approaches tailored for the UWP RichEditBox control:

Approach 1: Select All Text First (Simplest)

This reuses the exact logic you already use for selected text—you just expand the selection to cover every character in the document first:

// Reference your RichEditBox instance
var targetRichEdit = yourRichEditBoxControl;

// Select the entire document content
targetRichEdit.Document.Selection.StartPosition = 0;
targetRichEdit.Document.Selection.EndPosition = targetRichEdit.Document.TextLength;

// Apply your desired font color (example uses dark red)
targetRichEdit.Document.Selection.CharacterFormat.ForegroundColor = Colors.DarkRed;

This mirrors the manual "select all + format" workflow you’d use yourself, so it’s intuitive and easy to integrate with your existing code.

Approach 2: Preserve the User’s Original Selection

If you don’t want to disrupt the user’s current selected text while updating the whole document’s color, save the original selection bounds first, then restore them after formatting:

var targetRichEdit = yourRichEditBoxControl;

// Save the user's current selection positions
int originalSelectionStart = targetRichEdit.Document.Selection.StartPosition;
int originalSelectionEnd = targetRichEdit.Document.Selection.EndPosition;

// Format the entire document
targetRichEdit.Document.Selection.StartPosition = 0;
targetRichEdit.Document.Selection.EndPosition = targetRichEdit.Document.TextLength;
targetRichEdit.Document.Selection.CharacterFormat.ForegroundColor = Colors.DarkBlue;

// Restore the user's original selection
targetRichEdit.Document.Selection.StartPosition = originalSelectionStart;
targetRichEdit.Document.Selection.EndPosition = originalSelectionEnd;

Bonus: Set Default Color for New Text

If you want any new text typed into the RichEditBox to automatically use your chosen color, set the default character format alongside updating existing content:

// Set default format for future input (new text will inherit this color)
targetRichEdit.Document.DefaultCharacterFormat.ForegroundColor = Colors.DarkGreen;

// Format existing content using either Approach 1 or 2 above

All these methods work without extra libraries or complex parsing—they leverage the built-in RichEditBox document APIs you’re already familiar with.

内容的提问来源于stack exchange,提问作者Seungbin Oh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:26:03