有没有办法分组或暂时禁用 RichTextBox 的撤消历史? [英] Is there a way to group or temporarily disable the undo history for a RichTextBox?
问题描述
我目前正在处理 WPF 中 RichTextBox 中的表格.在 WPF 中,表没有行和列,它们只有行,每个行都有一定数量的单元格.当用户按下添加列"按钮时,我的程序会向每一行添加一个新单元格.
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?
推荐答案
如果您想分组撤消操作(而不是完全禁用撤消),您可以通过 TextBoxBase.BeginChange()
然后,在进行更改后,TextBoxBase.EndChange()
,即:
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();
}
或者,等效地,您可以调用 TextBoxBase.DeclareChangeBlock()
位于 using
语句:
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.
}
这篇关于有没有办法分组或暂时禁用 RichTextBox 的撤消历史?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!