Home > Mobile >  Listbox SelectItem out of List
Listbox SelectItem out of List

Time:07-06

I have a ListBox which shows Data from the Database. When clicking on a one of the items I would like to show the Data according to it in TextBoxes/ComboBoxes.

private void plantListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string value = plantListBox.SelectedItem.ToString(); //value just shows the name of the object and not the actual content here


        textbox_care.Text = value;
    }

While SelectedItem gives me all the following Data of the one Item I clicked in the ListBox. How can I distinguish all the Data and write it one by one?

 public class Plants
{
    public string ID { get; set; }
    public string Type { get; set; }
    public string LatPre { get; set; }
    public string LatPost { get; set; }
    public string EngName { get; set; }
    public string USDA { get; set; }
    public string Growth { get; set; }
    public string PH { get; set; }
    public string Moisture { get; set; }
    public string Height { get; set; }
    public string Location { get; set; }
    public string Info { get; set; }
    public string Origin { get; set; }
    public string Care { get; set; }
}

CodePudding user response:

from what I understand you're missing a cast:

private void plantListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string value = ((Plants) plantListBox.SelectedItem).Care ; // now you can acess the properties
    textbox_care.Text = value;
}

you do need to be careful with selection being null or having the wrong type

  • Related