简体   繁体   中英

Xamarin.Form Picker from List

I am using Xamarin.Form and I have this Picker in my xaml file:

<Picker Title="Select Communities"
        ItemsSource="{Binding areas}"
        ItemDisplayBinding="{Binding Description}" WidthRequest="400" x:Name="CommunityPicker" />

I am trying to bind this List I have: List<AreaClass> areas = new List<AreaClass>();

This list gets populated via api call and I can see it has 30 items in it, however nothing appears in my picker.

Here is my class:

public class AreaClass
    {
        public string Area { get; set; }
        public string Description { get; set; }
    }

and here is how its getting populated:

In code behind:

areas = webService.getAreas();

And here is the getAreas method:

public List<AreaClass> getAreas()
        {

            List<AreaClass> areas = new List<AreaClass>();

            try
            {
                using (connection = new SqlConnection(connection))
                {
                    connection.Open();

                    using (SqlCommand command = new SqlCommand("sp", connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;

                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {

                                AreaClass area = new AreaClass();

                                area.Area = reader["Area"].ToString();
                                area.Description = reader["Description"].ToString();

                                areas.Add(area);
                            }
                        }


                    }

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
            }
            finally
            {
                connection.Close();
            }

            return areas;

        }

What am I doing wrong? Why is my Picker not getting populated?

You can only bind to public properties. Either make areas a public property

public List<AreaClass> areas { get; set; }

or explicitly assign ItemsSource in the code behind

CommunityPicker.ItemsSource = webService.getAreas();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM