This is a migrated thread and some comments may be shown as answers.

SelectionChanged Event Firing When Not Supposed To

1 Answer 155 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Ristogod
Top achievements
Rank 2
Ristogod asked on 17 Feb 2011, 06:06 PM
We have this strange issue where we have a RadComboBox that we interact with from the code behind.

Basically in certain situations, we want to change the SelectedItem or SelectedValue without firing off our SelectionChanged Event Handler. So we are first Unsubscribing our event handler from the RadComboBox like this:

radComboBox.SelectionChanged -= new SelectionChangedEventHandler(ourEventHandler);

We then set our SelectedItem property to the value we want like so:

radComboBox.SelectedItem = outItem;

And then we resubscribe to the SelectionChanged event like so:

radComboBox.SelectedItem += new SelectionChangedEventHandler(ourEventHandler);

The idea is that while we set our SelectedItem, there should be no event set and nothing should fire. However, this is not the case. Instead as soon as we resubscribe to the event, the event fires all the same. Is this a bug?

1 Answer, 1 is accepted

Sort by
0
Ristogod
Top achievements
Rank 2
answered on 17 Feb 2011, 09:26 PM
Nevermind. I figured out what the problem was.

The RadComboBox was in another control, whose DataContext was changing. Even though the RadComboBox's ItemsSource and SelectedItem property were not bound to that same DataContext, when the parent's DataContext changed, it causes the RadComboBoxes DataContext to change.

Apparently when the RadComboBox DataContext changes, it fires the SelectionChanged event regardless. I'm not sure I understand why this is this way. To me the SelectionChanged should only fire when the SelectedIndex, SelectedItem or SelectedValue change explicitly.
Tags
ComboBox
Asked by
Ristogod
Top achievements
Rank 2
Answers by
Ristogod
Top achievements
Rank 2
Share this question
or