AutoCompleteBox: the OnLostFocus virtual method is not called
4 Answers 16 Views
Patrick
Top achievements
Rank 2
Iron
Patrick asked on 05 May 2021, 12:53 PM

Hello,

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

Sort by
0
Vladimir Stoyanov
Telerik team
answered on 07 May 2021, 09:53 AM

Hello Patrick,

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. 

Regards,
Vladimir Stoyanov
Progress Telerik

Т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.

0
Patrick
Top achievements
Rank 2
Iron
answered on 07 May 2021, 11:40 AM

Hi Vladimir,

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.

Regards.

0
Vladimir Stoyanov
Telerik team
answered on 12 May 2021, 08:33 AM

Hello Patrick,

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. 

Regards,
Vladimir Stoyanov
Progress Telerik

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/.

0
Patrick
Top achievements
Rank 2
Iron
answered on 12 May 2021, 09:08 AM

Hello Vladimir,

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...

Hello Patrick, 

Using the approach of clearing the SearchText in the LostFocus event is sufficient. I do hope you will find this applicable for your scenario. 

To reiterate, the OnLostFocus virtual method is not called due to the specific structure of the RadAutoCompleteBox and the fact the RadWatermarkTextBox inside it is focused initially.

- by
Vladimir Stoyanov
Telerik team
on 14 May 2021, 10:58 AM
Asked by
Patrick
Top achievements
Rank 2
Iron
Answers by
Vladimir Stoyanov
Telerik team
Patrick
Top achievements
Rank 2
Iron
Share this question
or