Home > Net >  Why is my AutoComplete suggestion dropdown blank
Why is my AutoComplete suggestion dropdown blank

Time:12-07

I have a Xamarin form where I am trying to add a SyncFusion AutoComplete control. The data is a simple class with only three string fields (CUSTNMBR, CUSTNAME, ZIP). I want it to match on any of the fields and display the coresponding CUSTNMBR. Here it my line in Xaml:

 <xForms:SfAutoComplete x:Name="customerAutoComplete" WidthRequest="120" BackgroundColor="White" />

In the form's code-behind constructor I call LoadCustomerData():

private async void LoadCustomerData()
{
    customerAutoComplete.DataSource = await GetCustomerCodes();
    customerAutoComplete.DisplayMemberPath = "CUSTNMBR";
    customerAutoComplete.SelectedValuePath = "CUSTNMBR";
    customerAutoComplete.SuggestionMode = SuggestionMode.Custom;
    customerAutoComplete.Filter = FilterCustomers;
    customerAutoComplete.AutoCompleteMode = AutoCompleteMode.Suggest;
    customerAutoComplete.Watermark = "Zip Code, Customer ID, or Customer Name";
    customerAutoComplete.MinimumPrefixCharacters = 3;
}

Here is my filter method.

private bool FilterCustomers(string search, object customer)
{
    var text = customerAutoComplete.Text;

    if (customer != null)
    {
        var myCustomer = (OrganizationSearchDto)customer;
        if (myCustomer.CustName.Contains(text) || myCustomer.CustNmbr.Contains(text) ||
            myCustomer.Zip.Contains(text))
        {
            return true;
        }
    }

    return false;
}

The above code worked partially when I had customerAutoComplete.SuggestionMode = SuggestionMode.Contains but it did not match on the other two fields. Now it still runs, however nothing is shown in the dropdown list (its blank). Why is my dropdown blank? Any hints, suggestion or a hard shove in the right direction will be appreciated.

CodePudding user response:

For anyone encountering this, tests to try:

  1. Put a breakpoint on return true - is that breakpoint hit for the customer(s) you expect to be shown as suggestions?

  2. Swap return true and return false, so it is true for all the OTHER customers - the opposite of what you want. See if it is still blank. If it is, then it isn't the filter - code elsewhere is interfering with display. Would need to show more code, or make a github containing a minimum repo that shows the problem.

  3. [from OP] The issue was that property names on DisplayMemberPath are case sensitive, as are the filter checks.

The fix for the filter was to ignore case everywhere. E.g.

if (myCustomer.CustName.ToLower().Contains(text.ToLower()) || ...)
  • Related