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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Can't Set Initial Selected Values on SfAutoComplete

Thread ID:

Created:

Updated:

Platform:

Replies:

143537 Mar 26,2019 03:25 AM UTC Mar 28,2019 01:10 PM UTC Xamarin.Forms 3
loading
Tags: SfAutoComplete
Jonas Dawson
Asked On March 26, 2019 03:25 AM UTC

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


Jonas Dawson
Replied On 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.

Jonas Dawson
Replied On 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!

Mugundhan Saravanan [Syncfusion]
Replied On 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. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon