SelectionChanged Event Firing When Not Supposed To

2 posts, 0 answers
  1. Ristogod
    Ristogod avatar
    63 posts
    Member since:
    Aug 2008

    Posted 17 Feb 2011 Link to this post

    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?
  2. Ristogod
    Ristogod avatar
    63 posts
    Member since:
    Aug 2008

    Posted 17 Feb 2011 Link to this post

    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.
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top