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

北慕城南 提交于 2019-12-05 12:37:05

问题


I'm currently wrestling with Tables inside RichTextBoxs in WPF. In WPF, tables don't have rows and columns, they just have rows, each having a certain number of cells. When a user presses the "Add Column" button, my program adds a new cell to each row.

The problem with using this method is after a user adds a column, if they press undo, it removes each cell one by one, obviously not what the user would expect.

Does anyone know a way to temporarily disable the addition of actions to the undo queue, or a way to group undo actions, or any other solution to my problem?


回答1:


If you want to group undo actions (rather than disable undo entirely), you can group a set of programmatic changes via TextBoxBase.BeginChange() then, after making the changes, TextBoxBase.EndChange(), i.e.:

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

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

Or, equivalently, you can call TextBoxBase.DeclareChangeBlock() inside a using statement:

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

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



回答2:


you can disable undo by setting IsUndoEnabled property to false or you can limit the undo by using UndoLimit. you can disable the undo by setting this property to 0, ie., UndoLimit="0"

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


来源:https://stackoverflow.com/questions/32042980/is-there-a-way-to-group-or-temporarily-disable-the-undo-history-for-a-richtextbo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!