Can't get Value from ComboBox

前端 未结 6 2133
时光取名叫无心
时光取名叫无心 2021-01-15 16:04

I have a simple comboBox with some Value/Text items in it. I have using ComboBox.DisplayMember and ComboBox.ValueMember to correctly set the value/text. When I try to get th

6条回答
  •  感情败类
    2021-01-15 16:47

    Not sure why SelectedValue doesn't return anything... I assume it could be due to the fact that you're not using data binding (DataSource). You should try to assign a list of cards to the DataSource property.

    Regarding the issue with SelectedItem : ComboBox.SelectedItem is of type Object, which doesn't have a property named Value. You need to cast it to the item's type ; but since it is an anonymous type, you can't... you should probably create a type to hold the value and text of the card, and cast to this type :

    Card card = ComboBox.SelectedItem as Card;
    if (card != null)
    {
        // do something with card.Value
    }
    

提交回复
热议问题