locked
How to get comboBox item value? RRS feed

  • Question

  • Hi all,

    I have a little problem. I have o idea what is wrong in my code. I was searchinng in net, but nothing I found.

    So simply I want to get comboBox selected item value  to textbox after I choose this item. So I have this ...

    private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (comboBox1.SelectedIndex > 0) { txtName.Text = comboBox1.SelectedItem.ToString();

    // OR this ... (with same result)

    txtName.Text = comboBox1.Text; } }

    BUT: the problem is that I am getting value like this ...

    System.Windows.Controls.ComboBoxItem: some_value

    What should I do? Thanks

    Wednesday, August 7, 2019 8:32 PM

Answers

  • finally  I found a solution ..

    So I have to put in selectionChanged funcion this

    txtName.Text = ((sender as ComboBox).SelectedItem as ComboBoxItem).Content as string;

    Wednesday, August 7, 2019 8:52 PM

All replies

  • finally  I found a solution ..

    So I have to put in selectionChanged funcion this

    txtName.Text = ((sender as ComboBox).SelectedItem as ComboBoxItem).Content as string;

    Wednesday, August 7, 2019 8:52 PM
  • My suggestion is to learn data binding.  Then you would have bound the SelectedItem from the Combobox to a property in your DataContext.  Then you will get notice that the item has changed in the setter of the property.

    Lloyd Sheen

    Wednesday, August 7, 2019 9:00 PM
  • Hi  TakeshiKitano,

    I am glad to know you solved. It would be appreciated if you could close the thread by marking your solution or helpful posts as an answer. This will help other members to find the solution quickly if they have faced the similar issue.

    Best regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 8, 2019 1:47 AM