The default databinding on TextBox
is TwoWay
and it commits the text to the property only when TextBox
lost its focus.
Is there any easy XAML way to make the databinding happen when I press the Enter key on the TextBox
?. I know it is pretty easy to do in the code behind, but imagine if this TextBox
is inside some complex DataTemplate
.
Here is an approach that to me seems quite straightforward, and easier that adding an AttachedBehaviour (which is also a valid solution). We use the default UpdateSourceTrigger (LostFocus for TextBox), and then add an InputBinding to the Enter Key, bound to a command.
The xaml is as follows
Then the Command methods are
And the TextBox is bound to the Property
So far this seems to work well and catches the Enter Key event in the TextBox.
I don't believe that there's any "pure XAML" way to do what you're describing. You can set up a binding so that it updates whenever the text in a TextBox changes (rather than when the TextBox loses focus) by setting the UpdateSourceTrigger property, like this:
If you set UpdateSourceTrigger to "Explicit" and then handled the TextBox's PreviewKeyDown event (looking for the Enter key) then you could achieve what you want, but it would require code-behind. Perhaps some sort of attached property (similar to my EnterKeyTraversal property) woudld work for you.
Answered here quite elegantly using attached behaviors, my preferred method for almost anything.
WPF how to make textbox lose focus after hitting enter
This is how I solved this problem. I created a special event handler that went into the code behind:
Then I just added this as a KeyUp event handler in the XAML:
The event handler uses its
sender
reference to cause it's own binding to get updated. Since the event handler is self-contained then it should work in a complex DataTemplate. This one event handler can now be added to all the textboxes that need this feature.This is not an answer to the original question, but rather an extension of the accepted answer by @Samuel Jack. I did the following in my own application, and was in awe of the elegance of Samuel's solution. It is very clean, and very reusable, as it can be used on any control, not just the
TextBox
. I thought this should be shared with the community.If you have a Window with a thousand
TextBoxes
that all require to update the Binding Source on Enter, you can attach this behaviour to all of them by including the XAML below into yourWindow
Resources
rather than attaching it to each TextBox. First you must implement the attached behaviour as per Samuel's post, of course.You can always limit the scope, if needed, by putting the Style into the Resources of one of the Window's child elements (i.e. a
Grid
) that contains the target TextBoxes.You can make yourself a pure XAML approach by creating an attached behaviour.
Something like this:
Then in your XAML you set the
InputBindingsManager.UpdatePropertySourceWhenEnterPressedProperty
property to the one you want updating when the Enter key is pressed. Like this(You just need to make sure to include an xmlns clr-namespace reference for "b" in the root element of your XAML file pointing to which ever namespace you put the InputBindingsManager in).