Problem Overview:
Any
KeyBinding
TextBox
TextBox
<Window>
<UserControl>
<Border>
<UserControl>
<TextBox>
<UserControl.Resources>
<RoutedUICommand x:Key="Commands.SomeCommand" />
</UserControl.Resources>
<UserControl.InputBindings>
<KeyBinding Key="A" Command="{StaticResource Commands.SomeCommand} />
</UserControl.InputBindings>
<UserControl.CommandBindings>
<CommandBinding Command="{StaticResource Commands.SomeCommand}" Executed="..." />
</UserControl.CommandBindings>
Command
KeyBinding
UserControl
A
TextBox.KeyDown
TextBox.PreviewKeyDown
A
Handled = false
TextBox.PreviewTextInput
TextBox.PreviewKeyDown
TextBox.KeyDown
TextBox.KeyDown
TextBox.PreviewTextInput
TextInput
and PreviewTextInput
only fires when the Text actually changes / might change.
As you updated your question to reflect, the Command
intercepts the event and the (Preview)TextInput events are never raised.
The nicest solution would be to add a modifier key to your KeyBinding, but I suspect that is not your preferred way to go.
Another option would be to e.Handle
the PreviewKeyDown event on the TextBox and raise the TextComposition events yourself, using something like:
target.RaiseEvent(new TextCompositionEventArgs(InputManager.Current.PrimaryKeyboardDevice,
new TextComposition(InputManager.Current, target, "A"))
{
RoutedEvent = TextCompositionManager.TextInputEvent
});
(Alternatively, insert into textBox.Text
at the correct CaretIndex
)
Truth be told, it would still be a hack.