在 RichEditBox 中包含当前撤消组中的文本更改?

Include text changes in current Undo group in RichEditBox?

我正在开发一个 UWP 应用程序,我正在使用 RichEditBox 控件,我在使用撤消功能时遇到了一些问题。

我知道我可以使用 RichEditBox 公开的 ITextDocument 对象中的 BeginUndoGroup 方法来创建一个我可以在编辑文本时使用的撤消组,这样用户就可以一起撤消所有这些更改(当我也调用 EndUndoGroup 时)。

我的问题是我的应用程序中有一些自动完成功能,但我不知道如何使我为自动完成添加的 characters/changes 包含在之前的撤消组中.

For example, say you enter the 'h' character, I detect that from the TextChanged event (or another event from the RichEditBox and I add "ello" to the text, so you end up with "hello", and then color the whole word in green.

If I use the BeginUndoGroup method, I can undo both the added "ello" and the green color, but the user will still end up with the 'h' character being there.

Or, if for example I color each new character in a random color, I have no idea how to make it so that the undo feature will undo both my color change and the previous character entered by the user.

有什么方法可以解决这个问题(即在 current/previous 撤消组中包含一些更改),还是我只需要想出一些肮脏的技巧,比如调用 Undo 当用户想要撤消更改时两次方法,希望他不会注意到文本闪烁?

感谢您的帮助!

看起来没有内置解决方案,所以我通过从 KeyDown 调用 BeginUndoGroup 解决了这个问题(实际上更多的是解决方法,但到目前为止它工作正常)事件处理程序,并在 TextChanged 处理程序中调用 EndUndoGroup

我也是 disabling/enabling 在应用语法高亮前后的 TextChanged 处理程序,否则在我应用文本格式时会执行该处理程序。

此外,完美地调用 Begin/EndUndoGroup 似乎并不重要,因为在错误的情况下调用其中任何一个(例如,调用 EndUndoGroup 时没有新的开放组,或者 BeginUndoGroup 连续两次)什么都不做,所以这就少了一件值得担心的事情。