AspiringApollo
AspiringApollo

Reputation: 53

Binding Picker ItemSource to List<string> not working Xamarin.forms MVVM

Firstly I'm retrieving an array from firebase cloud firestore. It's an array of strings. I then add those strings to a List. The issue is when I try to bind the pickers ItemSource to the list of strings it doesn't show the results when I click on the picker (its just blank). Please help me out here, I cannot seem to find a solution. Code below:

In the ViewModel (parts relevant):

public ObservableCollection<string> NamesListObsv { get; set; }

private List<string> _namesList;
public List<string> NamesList
    {
        get
        {
            return _namesList;
        }
        set
        {
            if (_namesList != value)
            {
                _namesList = value;
                OnPropertyChanged();
            }
        }
    }

NamesList = await GetNamesAsync();

NamesListObsv = new ObservableCollection<string>(NamesList);

public async Task<List<string>> GetNamesAsync()
    {
        try
        {

            var namesDoc = await CrossCloudFirestore.Current
                                                         .Instance
                                                         .Collection("names")
                                                         .Document("names_list")
                                                         .GetAsync();


            var namesList = namesDoc.ToObject<NamesModel>().NamesList;

            List<string> tempList = new List<string>();

            foreach (var names in namesList)
            {
                tempList.Add(name.ToString());
            }

            return tempList;

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return null;
        }          
    }

The Model:

public class NamesModel
{
    [Id]
    public string Id { get; set; }

    [MapTo("name_array")]
    public List<string> NamesList { get; set; }

}

The XAML (parts relevant):

<Picker x:Name="NamesListPicker"
                            Title="Select your Name.."
                            ItemsSource="{Binding NamesListObsv}"
                            ItemDisplayBinding="{Binding .}"/>

Yet all I get is an empty Picker even though the NamesListObsv contains three names ([0] "bob" [1] "jim" [2] "john"). Any advice would be GREATLY appreciated. Thanks all!

Upvotes: 0

Views: 1421

Answers (3)

Bandi T&#243;th
Bandi T&#243;th

Reputation: 167

  1. Make sure, GetNamesAsync() returns data
  2. Make sure, your ViewModel implements INotifyPropertyChanged interface
  3. Make sure, when you are assigning value to the variable named NamesListObsv the PropertyChanged event gets triggered.

As far as I see, you do not have Notify to the user interface, when the NamesListObsv gets a value.

Upvotes: 0

Cherry Bu - MSFT
Cherry Bu - MSFT

Reputation: 10356

Yet all I get is an empty Picker even though the NamesListObsv contains three names ([0] "bob" [1] "jim" [2] "john").

Don't use NamesListObsv = new ObservableCollection<string>(NamesList);, try to foreach NamesList to add data into NamesListObsv.

   NamesListObsv = new ObservableCollection<string>();
        for(int i=0;i<NamesList.Count;i++)
        {
            NamesListObsv.Add(NamesList[i]);
        }

Upvotes: 0

lidqy
lidqy

Reputation: 2463

Throw away NamesList and refactor NamesListObsv to:

private ObservableCollection<string> _namesListObsv;
public ObservableCollection<string> NamesListObsv 
    {
        get
        {
            return _namesListObsv;
        }
        set
        {
            if (_namesListObsv!= value)
            {
                _namesListObsv= value;
                OnPropertyChanged();
            }
        }
    }


NamesListObsv = new ObservableCollection<string>(await GetNamesAsync());

Upvotes: 1

Related Questions