0

I have two ComboBoxes in WPF. I have a bit crisis with their events. When I use the event of ComboBox1_SelectionChanged, I want to get two selectedValues, but ComboBox2 doesn't get value because there is no event How to solve this? Any suggestion?

private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   if (ComboBox1.SelectedValue == "Shoe" && ComboBox2.SelectedValue == "Nike")
   {
      //TODO
   }
}
Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
user1358072
  • 447
  • 2
  • 12
  • 26
  • 2
    Not sure if I follow. You should be able to query for the selected value of any combobox whenever you want; you don't have to listen to the event. – Chris Sinclair Jul 08 '13 at 12:29
  • okay...can you show me your code example, please!! thanks – user1358072 Jul 08 '13 at 12:32
  • Unfortunately, my code example probably would have been exactly what you posted. What exactly is going wrong? Have you placed a debugger breakpoint and checked that the values are what you expect? Is the code in your `//TODO` not running? Have you _tried_ to run the code you posted? – Chris Sinclair Jul 08 '13 at 12:33
  • What wrong is : when i select "Shoe", the event will be fired and it will get value "Dinner". But when I select both "Shoe" and "Nike", there is no SelectedValue of "Nike" so it wont pass if statement. hope u understand.... – user1358072 Jul 08 '13 at 12:38
  • How can you select the values at the same time? Fastest mouse fingers in the west? Have you double-checked that you're inserting the correct label/values into the combo boxes when you build your GUI? – Chris Sinclair Jul 08 '13 at 12:39
  • yeah but you know what i mean. Sorry my english bad. I mean when I select BOTH "Shoe" from ComboBox1 and "Nike" from ComboBox2, i only get SelectedValue "Shoe" but not "Nike" so it wont pass the if statement. – user1358072 Jul 08 '13 at 12:41
  • 1
    You should wire up the same event handler to the Combobox2. The event handler will then be fired once each combobox changes. – Guilherme Duarte Jul 08 '13 at 12:44

1 Answers1

0

Hi user1358072 I think you should set SelectedIndex="0" of combobox2 in xaml code.

It is working on my side .

If you want to handle event for both combo box create event handler for both. and set

SelectedIndex="0" for both.

I think it will useful for You

Rajeev Ranjan
  • 1,006
  • 8
  • 18
  • good idea... i am not sure how to create event handler for both. maybe you dont mind showing me your code example so that i would get a picture in my mind. thanks so much for your time :) – user1358072 Jul 08 '13 at 12:44
  • private void comboBox1_SelectionChanged_1(object sender, SelectionChangedEventArgs e) { if (comboBox1.SelectedValue == "Shoe" && comboBox2.SelectedValue == "Nike") { //TODO } } private void comboBox2_SelectionChanged_1(object sender, SelectionChangedEventArgs e) { if (comboBox1.SelectedValue == "Shoe" && comboBox2.SelectedValue == "Nike") { //TODO } } – Rajeev Ranjan Jul 08 '13 at 12:49