UWP/WinRT: How to enable undo hooks on a TextBox?

2019-05-30 13:54发布

The TextBox class already supports undo, as it is present and functional in the context menu.

I would like to implement undo/redo buttons as found in every common document editor such as Microsoft Word. They would be disabled when they have no action to take, and when there is an undo/redo stack to move through, pressing the buttons would cause the TextBox's contents to undo and redo.

Looking at the TextBox API, there doesn't seem to be any mention of how to hook into the undo data. The only discussion is a mention that undo is present on the context menu.

How are undo/redo hooks implemented on a TextBox?

If it makes a difference, I'm currently coding in C++/CX.

1条回答
做自己的国王
2楼-- · 2019-05-30 14:16

You definitely can record the history manually by TextChanged event. Undo command is used to display the previously input. Hook into the control seems not possible.

Handle the ContextMenuOpening event from TextBox and you can modify the Popup by your own commands, for example your own undo/redo history.

A good sample: https://code.msdn.microsoft.com/windowsapps/Context-menu-sample-40840351 also works fine with UWP.

查看更多
登录 后发表回答