I have a window with a bunch of TextBoxes bound to properties of ViewModel. All bindings have UpdateSourceTrigger
set to LostFocus
(this is neccessary, I can't change it to PropertyChanged
).
The form can be submitted using a button, which works fine. Focus is removed from the form and all bindings are updated before processing the form.
On the other hand, it can also be submitted using shortcut (Ctrl+Enter). I'm using KeyBinding
for this. Pressing the shortcut keys doesn't remove focus from the currently editing TextBox and its binding doesn't update.
The workflow is like this: User types in some text into a TextBox and immediately presses Ctrl+Enter. The KeyBinding invokes the SubmitCommand which calls the Save method. In the Save method, I can only access the old value, because the binding was not updated, because focus was not lost.
The ViewModel is an abstract base class with multiple implementations, so it has no knowledge of the window (view).
I tried using Keyboard.ClearFocus()
, but it doesn't update the binding. I believe it doesn't remove logical focus and thus doesn't trigger LostFocus
event.
Working (but ugly) solution is to set the focus to another control, but I'd like to avoid that.
<KeyBinding Modifiers="Control" Key="Enter" Command="{Binding SubmitCommand}" />
public ICommand SubmitCommand { get; }
//constructor
public ViewModel()
{
SubmitCommand = new RelayCommand(Save);
}
private void Save()
{
//I need to update the bindings here
}
My expectation: Programmatically clear logical focus to update the bindings.