有没有办法分组或暂时禁用 RichTextBox 的撤消历史记录?

Is there a way to group or temporarily disable the undo history for a RichTextBox?

我目前正在研究 WPF 中 RichTextBox 中的表格。在 WPF 中,表格没有行和列,它们只有行,每行都有一定数量的单元格。当用户按下 "Add Column" 按钮时,我的程序会向每一行添加一个新单元格。

使用此方法的问题是在用户添加一列后,如果他们按撤消,它会一个一个地删除每个单元格,这显然不是用户所期望的。

有谁知道暂时禁止向撤消队列添加操作的方法,或将撤消操作分组的方法,或解决我的问题的任何其他方法?

您可以通过将 IsUndoEnabled 属性 设置为 false 来禁用 undo,或者您可以使用 UndoLimit 来限制撤消。您可以通过将此 属性 设置为 0 来禁用撤消,即 UndoLimit="0"

<RichTextBox  Name="myRitchTextBox" IsUndoEnabled="False" />

如果您想分组撤消操作(而不是完全禁用撤消),您可以通过TextBoxBase.BeginChange() then, after making the changes, TextBoxBase.EndChange()对一组程序更改进行分组,即:

        richTextBox.BeginChange();
        try
        {
            // Add column

            // For each row, add a cell to the column.
        }
        finally
        {
            richTextBox.EndChange();
        }

或者,等效地,您可以调用 TextBoxBase.DeclareChangeBlock() inside a using 语句:

        using (richTextBox.DeclareChangeBlock())
        {
            // Add column

            // For each row, add a cell to the column.
        }