Command for WPF TextBox that fires up when we hit Enter Key
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In WPF, the clean MVVM-style way to react to Enter in a TextBox is usually to bind that key to an ICommand. The main design decision is not only how to fire the command, but also whether Enter belongs to the TextBox itself, to the whole form, or to multiline text editing.
Bind Enter with a KeyBinding
If the TextBox owns the Enter behavior, a KeyBinding is the direct approach:
This keeps the trigger declarative and lets the view model own the action logic.
A Minimal Relay Command
To make that work, the view model needs an ICommand implementation:
And then use it in the view model:
This keeps the behavior testable and aligned with MVVM.
Multiline TextBoxes Need a Different Rule
If the TextBox is multiline, Enter may mean "insert a new line" instead of "submit." In that case, be explicit about the intended behavior.
For a single-line submit field:
If your design needs both newline entry and a send action, define a deliberate rule such as Enter to submit and Shift+Enter for newline, or the reverse. Without that decision, the control behavior feels inconsistent.
Alternative: Use a Default Button
Sometimes the TextBox should not own Enter handling at all. If the whole form has one obvious submit action, a default button may be simpler:
This makes Enter trigger the button when focus is in the window. It is often the better design when multiple controls participate in one form submission.
Keep the Bound Value Current
If the command depends on the latest text, make sure the binding updates when you expect. Using UpdateSourceTrigger=PropertyChanged is often the simplest way to ensure the view model sees the latest text before the Enter command runs.
Without that, the command can execute against stale data if the binding normally updates only when focus changes.
Avoid Mixing Event Handlers and Commands
You can handle KeyDown in code-behind, but that usually makes the view harder to test and scatters input logic across the UI layer. If the action belongs to the view model, commands are cleaner.
Use code-behind only when the logic is genuinely view-specific and does not belong in the command model.
Common Pitfalls
- Handling Enter only in code-behind when the action belongs in an
ICommand. - Forgetting about multiline behavior and wondering why Enter inserts a newline instead of submitting.
- Wiring both a textbox
KeyBindingand a default button to the same command and accidentally executing it twice. - Letting the text binding update too late, so the command sees an old value.
- Treating every Enter key the same when the UI really needs different rules for single-line and multiline input.
Summary
- A
KeyBindingto anICommandis a clean WPF solution for Enter in aTextBox. - Keep execution logic in the view model when using MVVM.
- Decide explicitly whether Enter means submit or newline.
- A default button can be better when the whole form shares one submit action.
- Make sure the bound text updates before the command executes.

