If we follow the WPF good practices, the `OnLostFocus` method must be called when the control loses focus and it's the job of this method to raise the `LostFocus` event.
In the case of the AutoCompleteBox, the event is raised, but the method is not called. So it's not possible to do some work before the event is raised.
4 Answers, 1 is accepted
Thank you for the shared information.
Generally speaking, the RadAutoCompleteBox does not manually implement the OnLostFocus method or alter its behavior. It is inherited from the UIElement native class, which is a parent of the RadAutoCompleteBox.
That said, you can consider overriding the OnPreviewLostKeyboardFocus method, which should also be fired before the LostFocus event. Do give this a try and let me know, if it helps.
Тhe web is about to get a bit better!
The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.
Thank you for your answer.
OnPreviewLostKeyboardFocus is not the same as OnLostFocus: the first one is only for keyboard, the second one is for the application focus, that doesn't change if you switch to another application and then return to the first one. In addition OnPreviewLostKeyboardFocus is a preview event: it is fired even if the event is canceled.
What I want to do is to force the user to select only a value in the auto-complete box and to show it by clearing the text field when the focus is lost. In Silverlight (and perhaps in old versions ot Telerik UI for WPF), it was working by overloading the OnLostFocus method. Now, I had to add a handler to the LostFocus event, that's not the same thing.
Even if OnLostFocus is inherited from UIElement, it would be good practice to use it as intended, not to only raise the event.
I searched our backlog and found the following thread, where this scenario was discussed: OnLostFocus virtual method not called. To summarize, the textbox element inside the RadAutoCompleteBox is focused initially and its OnLostFocus method is called when the focus is lost.
With this in mind, can you elaborate a bit on what you are trying to achieve so that I can better assist you? Can you elaborate on what you mean by "force the user to select only a value in the auto-complete box and to show it by clearing the text field when the focus is lost"? This will hopefully allow me to understand the requirements and suggest an alternative approach.
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.
I have the following code (in RemObjects Oxygene), that I think is better in the OnLostFocus but I have added on the LostFocus event as a workaway:
LostFocus += -> Begin If Not Assigned (SelectedItem) Then SearchText := String.Empty End;
It's just that the control is not following the WPF pattern for events and their methods and it's a lost of time to search why it doesn't work as it should...