Controllo AutoComplete

Il controllo AutoComplete permette di cercare e selezionare un valore a partire da una sorgente dati. Il valore selezionato devono essere presenti nella sorgente dati. Utile quando la sorgente dati contiene molti elementi che renderebbero il form troppo lungo o lento se visualizzati contemporaneamente.
 
Personalizzazione
Possiamo impostare una sorgente dati personalizzata specificando il valore ControlClass per DataSourceMode e inserendo namespace e nome classe nella proprietà del controllo CustomControlClass.
 
Nel metodo GetListValuesAsync dobbiamo gestire sia la fase di recupero dei suggerimento (parametro Title) sia quella di recupero, validazione e salvataggio del relativo valore (parametro Text).
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using DataWeb.Data;
using DataWeb.Data.Controls;
using DataWeb.Identity;
using DataWeb.Structure;
using MyApp.Infrastructure;
using MyApp.Structure;

namespace MyApp.DataWeb.Data.Controls
{
    public class Project_CompanyIdMaster : AutoComplete
    {
        private readonly CompanyStore companyStore;

        public Project_CompanyIdMaster(Form form, IServiceProvider serviceProvider) : base(form, serviceProvider)
        {
            companyStore = serviceProvider.GetService<CompanyStore>();
        }

        public override async Task<IEnumerable<List.ListItem>> GetListValuesAsync(Dictionary<string, object> parameters, IUser user, string itemId = null, NavigationContext navigationContext = null)
        {
            var companies = Enumerable.Empty<Company>();

            if (parameters.ContainsKey("Value"))
            {
                // Get selected items
                companies = await companyStore.GetCompaniesAsync(new CompanyStore.Filter { IdMaster = Convert.ToString(parameters["Value"]) });
            }

            if (parameters.ContainsKey("Title"))
            {
                // Get suggested items
                companies = await companyStore.GetCompaniesAsync(new CompanyStore.Filter { SearchText = Convert.ToString(parameters["Title"]) });
            }

            return companies.Select(x => new List.ListItem { Title = x.Name, Value = x.IdMaster, AdditionalValues = new List<List.ListItem.AdditionalValue>() });
        }
    }
}