Search code examples
c#wpfeventstabslost-focus

WPF - Trigger "LostFocus" on currently active element in Tab before Tab Change


I have a TabControl, and each TabItem has multiple, data-bound textboxes. When I edit an text in one box, and the switch the tab directly (without leaving the textbox), the change is lost. Apparently, the "LostFocus" Event is not triggered when changing the tab, so the new value is not written to the bound property.

How can I trigger the "LostFocus" on the currently active (or all) elements in the active tab before changing over to the new tab?

I could surely do an override and call the event manually on all elements, but that would be quite unhandy because one has to update that list every time a field is added.

Is there some way to do it automatically?


Solution

  • Use UpdateSourceTrigger.PropertyChanged instead of LostFocus and set the binding Delay property to an interval of your choice.