This is a migrated thread and some comments may be shown as answers.

Show all items when SearchText is an empty string

6 Answers 653 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Christoph
Top achievements
Rank 1
Veteran
Christoph asked on 16 Mar 2021, 09:07 AM

Hello,

how would I go about if I wanted the RadAutoCompleteBox to show all items if the SearchText is empty instead of no items at all?

I had my desired behaviour already implemented using the RadComboBox, which was perfectly fine, despite the fact that I'm dealing with mutliple thousands of items easily and the AutoCompleteBox seems to deal with those amounts of items better than the ComboBox.

What I want is a AutoComplete box that opens upon getting the focus (or upon pressing Ctrl + Space). If the SearchText is populated I want the dropdown to contain the filtered list. But if the SearchText is empty I want the dropdown to show all the entries. Currently it shows none.

To open the dropdown on focus and on ctrl+space is not the problem. Only the contents of the dropdown.

I found guides for silverlight (https://www.telerik.com/forums/display-all-items-on-focus) aswell asp.net (https://www.telerik.com/support/kb/aspnet-ajax/autocompletebox/details/show-all-records-when-focusing-the-input-element-of-autocompletebox) but was not able to adept those solutions fopr WPF.

6 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 19 Mar 2021, 08:10 AM

Hello Christoph,

To achieve your requirement, you can use a custom filtering behavior. This will allow you to provide all items as filtered when the search text is empty. Then you can use the GotFocus and KeyDown events to call the Populate() method of RadAutoCompleteBox with an empty string. I've attached a small example showing this approach. Can you please give it a try and let me know if it helps?

Regards,
Martin Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Paul
Top achievements
Rank 1
answered on 19 Mar 2021, 12:45 PM

I am looking for exactly the same functionality.

When I tried the example, it appears to work fine. However when I try to select anything from the list when the search text is empty, the selection is not accepted. The Populate() method seems to prevent that.

0
Paul
Top achievements
Rank 1
answered on 19 Mar 2021, 01:01 PM
When selecting with a mouse click, with keyboard (ENTER) it still works.
0
Martin Ivanov
Telerik team
answered on 23 Mar 2021, 10:12 AM

Hello Paul,

You are right. When an item is selected the GotFocus event fires and the custom logic prevents the selection. To resolve this, you can replace the GotFocus usage in the example with MouseLeftButtonDown. For example:

autoCompleteBox.AddHandler(RadAutoCompleteBox.MouseLeftButtonDownEvent, new MouseButtonEventHandler(Acb_MouseLeftButtonDown), true);

private void Acb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
	var source = (FrameworkElement)e.OriginalSource;
	var input = source.ParentOfType<RadWatermarkTextBox>();
	if (input != null && string.IsNullOrEmpty(input.Text))
	{
		acb.Populate(string.Empty);
	}
}

I hope this helps.

Regards,
Martin Ivanov
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
Paul
Top achievements
Rank 1
answered on 23 Mar 2021, 11:42 AM

Thanks a lot, this works just like expected!

One note however: at first I added the event by modifying the XAML and like so:

<telerik:RadAutoCompleteBox x:Name="acb" Width="300" VerticalAlignment="Center" MouseLeftButtonDown="acb_MouseLeftButtonDown" />

 

But that did not work, the event only fired when I clicked the edge of the control, not inside it. And when fired it did not open the dropdown list.

But then I noticed that you added the handler in a different way and used that. I do find it a bit weird that it doesn't work when the event is defined from XAML, shouldn't that be the same?

 

0
Martin Ivanov
Telerik team
answered on 23 Mar 2021, 01:15 PM

Hello Christoph,

Yes, adding the event handler for MouseLeftBUttonDown in XAML won't work. This is because the event is marked as handled internally by the AutoCompleteBox control which means that it won't bubble to handlers that are not attached with the "handledEventstToo" parameter set to True. And to set this parameter you need to use the AddHandler() method in code. In the code snippet from my previous reply, you will notice that I've passed "true" as the last argument of the method.

Regards,
Martin Ivanov
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/.

Tags
AutoCompleteBox
Asked by
Christoph
Top achievements
Rank 1
Veteran
Answers by
Martin Ivanov
Telerik team
Paul
Top achievements
Rank 1
Share this question
or