We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Can't Set Initial Selected Values on SfAutoComplete

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; }
}


3 Replies

JD Jonas Dawson March 26, 2019 01:05 PM UTC

Some additional information:

  1. The DataSource is properly binding with the Models property in the ViewModel and correctly displaying the full list of Models in the dropdown.

  2. I added a ValueConverter to the SelectedItem binding on the SfAutoComplete control, using the following code:

        autoComplete.SetBinding(SfAutoComplete.SelectedItemProperty, "SelectedTags", BindingMode.TwoWay, new ArbitraryValueConverter());

    I set a breakpoint within the converter, and verified that the property is properly binding with SelectedModels in the ViewModel class.

  3. On load, despite the binding working, no items appear selected in the control. Additionally, items in SelectedModels are still in the control's dropdown despite IsSelectedItemsVisibleInDropDown being set to "false".

  4. Once I select an item from the dropdown, SelectedModels on the ViewModel is properly updated with the newly selected Model(s), and the selected Model(s) are removed from the dropdown. However, any Models that I attempted to pre-set at instantiation in the SelectedModels property on the ViewModel are gone.


JD Jonas Dawson March 26, 2019 03:27 PM UTC

I figured it out!!!

On navigation, I was setting the SelectedModels property on the ViewModel, but the Models were not being populated until after ViewModel construction via a web service call. What is clear now is that, at the time of initial appearance, it's important for both the DataSource and the SelectedItem collections to be populated.

I hope this helps someone!


MS Mugundhan Saravanan Syncfusion Team March 28, 2019 01:10 PM UTC

Hi Jonas Dawson,


Thanks for the Update.


We are glad to know that the issue is resolved from our side. Please get back to us if you have any further assistance on this.


Regards,
Mugundhan S. 


Loader.
Live Chat Icon For mobile
Up arrow icon