I have text box with AcceptsReturn
enabled. I would like to insert a new line after pressing Shift + Enter.
Textbox XAML looks like this:
...
AcceptsReturn="True"
cal:Message.Attach="[Event KeyDown] = [Action HandleInput($eventArgs)]"
And the HandleInput method:
public void HandleInput(KeyEventArgs keyArgs)
{
if (keyArgs.Key == Key.Enter && Keyboard.Modifiers != ModifierKeys.Shift)
{ ...
When i press only Enter it should make something with input but it insert new line every time.
It's because KeyDown
uses event bubbling which means that the event is first raised on the control, and then its parent, and that parent's parent and so-forth until the event is handled.
When you set AcceptsReturn
to true, the TextBox control handles the enter keystroke so the event is not "bubbled" to the HandleInput method.
You can use PreviewKeyDown
instead which uses event tunneling, the opposite of event bubbling.