is there a way to update a binding before or when a command is triggered? I have some text fields I can edit and save using a command, accessible via a keyboard shortcut. As the binding is usually only updated when the text field loses focus, the last change is not kept when pressing the key to save the data. Instead I have to tab out of the text field first to make it update and then save it.
Is there a way to force the update in an elegant way? I am using MVVM (but not any MVVM framework), so I’d like to keep UI specific things out of the command code. Also I don’t really want to change the binding to update on every change, it’s fine to have it update only when the focus is lost.
I have solved this now by explicitely setting the focus to some other element before asking for the values. This obviously makes the element that currently has the focus lose it and update the binding.
To set the focus, I have written an attached property, inspired by answers on other questions. Also, together with my other question I made this somewhat automated.
So to use it, I basically attach my property to an element, in this case a tab control:
In my view model, I have a simple boolean property
ShouldClearFocusthat is a standard property raising aPropertyChangedEvent, so data binding works. Then I simply setShouldClearFocustotruewhen I want to reset the focus. The attached property automatically sets the focus and resets the property value again. That way I can keep settingShouldClearFocuswithout having to set it tofalsein between.The attached property is a standard implementation with this as its change handler: