I'm trying to have certain values pre-selected in the SfAutoComplete control. I referred to
this post for guidance. Unfortunately, I'm not able to load the control with values already selected.
The relevant code is below. Thanks in advance for any help you can give!
AutoComplete Control
var autoComplete = new SfAutoComplete
{
DisplayMemberPath = "name",
MaximumDropDownHeight = 350,
ShowSuggestionsOnFocus = true,
Watermark = "Select models...",
MultiSelectMode = MultiSelectMode.Token,
TokensWrapMode = TokensWrapMode.Wrap,
IsSelectedItemsVisibleInDropDown = false,
TokenSettings = new TokenSettings
{
FontSize = 14,
BackgroundColor = Colors.Black,
TextColor = Colors.White,
DeleteButtonColor = Colors.Red,
IsCloseButtonVisible = true,
DeleteButtonPlacement = DeleteButtonPlacement.Right,
CornerRadius = 3,
HorizontalOptions = LayoutOptions.Center
}
};
autoComplete.SetBinding(SfAutoComplete.SelectedItemProperty, "SelectedModels");
autoComplete.SetBinding(SfAutoComplete.DataSourceProperty, "Models");
ViewModel
public class ViewModel
{
private ObservableCollection<Model> _models;
public ObservableCollection<Model> Models
{
get => _models;
set
{
_models = value;
OnPropertyChanged(nameof(Models));
}
}
private ObservableCollection<object> _selectedModels;
public ObservableCollection<object> SelectedModels
{
get => _selectedModels;
set
{
_selectedModels = value;
OnPropertyChanged(nameof(SelectedModels));
}
}
public ViewModel(ModelRetriver modelRetriever)
{
var models = modelRetriever.GetAllModels();
var selectedModels= modelRetriever.GetSelectedModels();
Models = new ObservableCollection<Model> { models};
SelectedModels = new ObservableCollection<object> { selectedModels };
}
public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
// Functioning OnPropertyChanged implementation, not important for the example
}
}
Model
public class Model
{
public string name { get; set; }
public Guid account {get; set; }
}