1

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.

Adam Štafa
  • 353
  • 1
  • 13
  • 2
    Instead of playing with focus, you could define a `BindingGroup` and then call `CommitEdit` of that group. Also, see [this question](https://stackoverflow.com/questions/57493/wpf-databind-before-saving). – dymanoid Aug 08 '19 at 11:56
  • Thanks for fast response. The application architecture wasn't really suited for using BindingGroup (I didn't even know it existed before), but the linked question helped me to arrive to a solution. [This answer](https://stackoverflow.com/a/4724766/11755813) was easy to implement and worked in all cases. Also it doesn't require any reference to the view. – Adam Štafa Aug 08 '19 at 12:27
  • 1
    If you use `Keyboard.FocusedElement` in the view-model, then your view-model indeed has knowledge about a particular view aspect - `Control`s. A view-model should not know about its view, that is correct. But furthermore, a view-model should not operate with any view-related types or services, like `Keyboard.FocusedElement`. You make your view-model not testable with this. It breaks MVVM. – dymanoid Aug 08 '19 at 12:31

0 Answers0