I have a WPF application with the following KeyBinding on its main window:
<KeyBinding Command="Commands:EditCommands.Undo" Gesture="CTRL+Z" />
<KeyBinding Command="Commands:EditCommands.Redo" Gesture="CTRL+Y" />
This makes the command respond to the shortcut fine. However, in all the places where I have embedded WinForms text boxes or rich text boxes, I've lost the ability to use those shortcuts. If I remove the above bindings, the WinForms shortcuts work fine.
How can I support these shortcuts in both WinForms and WPF? I'd prefer a generic method since this problem is likely to affect many other commands with the same keybindings.
I'm puzzled why you aren't using the built-in commands:
ApplicationCommands.Undo
, andApplicationCommands.Redo
There are several advantages to using these built-in commands:
TextBox
and RichTextBox
So if possible you should use the built in ApplicationCommands
by simply registering CommandBindings
for them at the appropriate places in your code.
More information
If you use the built in undo/redo functionality in both WPF and WinForms, it just works. For example, the following creates two RichTextBoxes
, one based on WinForms and one on WPF, and both have full undo/redo capabilities:
<UniformGrid Columns="2"
xmlns:winforms=
"clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms">
<WindowsFormsHost >
<winforms:RichTextBox />
</WindowsFormsHost>
<RichTextBox />
</UniformGrid>
Since this works and yours doesn't, try to figure out what is different. You said in your comments you tried removing the custom WPF InputBindings
. Have you done the same on the WinForms side? If not, please try it, or if that isn't possible please edit your question to show that code as well.
Note that you can remap ApplicationCommands
into your own RoutedCommands
: Just add a CommandBinding
and in the handler fire your custom RoutedCommand
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With