WPF: C# How to get selected value of Listbox?

前端 未结 5 1737
忘掉有多难
忘掉有多难 2021-01-27 17:00

I am trying to fetch the selected value of the listbox. When I actually try LstGroup1.SelectedItem I get the value { BoothID = \"4\", BoothName = \"HP\" }

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-27 17:16

    I think, you should try one of the following : //EDIT : This solution only works, if Booth is a class/struct

    var myBooth = LstGroup1.SelectedItem as Booth;
    String id =myBooth.BoothID;
    String name=myBooth.BoothName:
    

    or use a generic list with a different type :

    public List BoothsInGroup1 { get; set; }
    ....
    var myBooth = LstGroup1.SelectedItem;
    String id =myBooth.BoothID;
    Stringname=myBooth.BoothName:
    

    And if Booth isn't a class yet, add a new class:

    public class Booth
    {
        public int BoothID { get; set; }
        public String BoothName { get; set; }
    }
    

    So u can use it in your generic list, an you can fill it after databasereading

    BoothsInGroup1.Add(new Booth
       { 
         BoothID = Convert.ToInt32(da["BoothID"]),
         BoothName = da["BoothName"].ToString() 
       });
    

提交回复
热议问题