简体   繁体   中英

Items in picker from a list - Xamarin Code behind

I am stuck on how to show items from a list in a picker.

I can use a for loop and add the items to the Picker, is there any other way ? I have to also use bound values from the list. All I get right now is the type and not the ID or Category name i need.

Image of what I see... 我看到的图像...

        private async void GetCategories(string url)
    {
        //get json as a string
        var result = await _clientHttp.GetStringAsync(url);


        var json = JsonConvert.DeserializeObject<List<CategoryList>>(result);

        List<CategoryList> tempList = new List<CategoryList>();
        foreach (var items in json)
        {

          tempList.Add(new CategoryList{CatId = items.CatId,category = items.category});

        }
        ;

        PickerCategory.ItemsSource =  tempList;


    }

You must override your CategoryList.ToString() method to display the appropriate values:

class CategoryList{

    public string category {get;set;}

    public override string ToString(){
         return category;
    }
}

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