My setting is as described below:
- WPF 4 application
- MVVM Lite framework
- An “Add Item” window is shown on top of the main window in dialog mode (using view.ShowDialog();)
- The dialog has a number of input fields, and the “Save” button that has it IsDefault property set to True
- “Save” button is handled using a binding to a Command
- Data binding is defined between the input fields in view’s XAML and the corresponding properties in the ViewModel (one way, for the UI to update the ViewModel, with Mode=OneWayToSource)
The problem is that when I press Enter on the keyboard, the value that I provided in the last input field is not pushed to the underlying property of the ViewModel.
I suspect that this has something to do with the fact that the input field hasn’t lost the focus before the window is closed (and thus all the bindings are “dissolved”).
For comparison, if I click the “Save” button (instead of letting its Click being handled by the window on Enter), the value is updated in the property.
Moreover, if I add (horror! horror!) an event handler to the button’s Click event, and call button.Focus() in code-behind, everything works!
What can be the remedy?
I obviously don’t want to handle any window closing events, and “manually” fetch the missing values… This would violate the whole MVVM concept 🙁
Any better suggestions?
Thank you,
Alex
By default, a
TextBoxonly notifies it’s source that its value has changed once it loses focus. You can change that in your binding by settingUpdateSourceTrigger=PropertyChanged. This will make the TextBox send an update notification to its source its Text gets changed instead of only when it loses focus.If you don’t want to send an update notification anytime a key is pressed, you can create an
AttachedPropertyto update the source if the Enter key is pressed.Here’s the AttachedProperty that I use for situations like this: