在执行 Ctrl-Z 时,在 TextChanged 中移动焦点会导致 "Cannot Undo or Redo while undo unit is open."

Moving focus in TextChanged results in "Cannot Undo or Redo while undo unit is open." when doing Ctrl-Z

我有 TextBox1 和 TextBox2,TB1 带有 OnTextChanged 处理程序,我将焦点移至 TB2。

现在,如果我再次手动聚焦 TB1 并执行 Ctrl-Z,我会收到消息 "Cannot Undo or Redo while undo unit is open."。任何人? :)

这可能对其他人有用。

我在网上搜索并多次看到这条消息,但与我的 situation/no 解决方案没有任何相似之处,但我确实在 MSDN for the TextBox.Undo() method 看到了以下内容:

"The Undo method does not work with the KeyPress or TextChanged events."

我现在尝试的是使用 BeginInvoke 进行聚焦异步。

private void TB1_TextChanged(object sender, TextChangedEventArgs e)
{
    Dispatcher.BeginInvoke((Action)FocusTB2);
}

public void FocusTB2()
{
    TB2.Focus();
}