title: How to cancel a ComboBox SelectionChanged event?
body: <p>Is there an easy method to prompt the user to confirm a combo box selection change and not process the change if the user selected no?</p>
  
  <p>We have a combo box where changing the selection will cause loss of data.  Basically the user selects a type, then they are able to enter attributes of that type.  If they change the type we clear all of the attributes as they may no longer apply. The problem is that to under the selection you raise the <code>SelectionChanged</code> event again.</p>
  
  <p>Here is a snippet:</p>
  
  <pre><code>if (e.RemovedItems.Count &gt; 0)
  {
      result = MessageBox.Show("Do you wish to continue?", 
          "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning);
  
      if (result == MessageBoxResult.No)
      {
          if (e.RemovedItems.Count &gt; 0)
              ((ComboBox)sender).SelectedItem = e.RemovedItems[0];
          else
              ((ComboBox)sender).SelectedItem = null;
      }
  }
  </code></pre>
  
  <p>I have two solutions, neither of which I like.</p>
  
  <ol>
  <li><p>After the user selects <strong>'No'</strong>, remove the <code>SelectionChanged</code> event handler, change the selected item and then register the <code>SelectionChanged</code> event handler again. This means you have to hold onto a reference of the event handler in the class so that you can add and remove it.</p></li>
  <li><p>Create a <code>ProcessSelectionChanged</code> boolean as part of the class.  Always check it at the start of the event handler.  Set it to false before we change the selection back and then reset it to true afterwards.  This will work, but I don't like using flags to basically nullify an event handler.</p></li>
  </ol>
  
  <p>Anyone have an alternative solution or an improvement on the ones I mention?</p>