title: WPF: Cancel a user selection in a databound ListBox?
body: <p>How do I cancel a user selection in a databound WPF ListBox? The source property is set correctly, but the ListBox selection is out of sync.</p>
  
  <p>I have an MVVM app that needs to cancel a user selection in a WPF ListBox if certain validation conditions fail. Validation is triggered by a selection in the ListBox, rather than by a Submit button. </p>
  
  <p>The <code>ListBox.SelectedItem</code> property is bound to a <code>ViewModel.CurrentDocument</code> property. If validation fails, the setter for the view model property exits without changing the property. So, the property to which <code>ListBox.SelectedItem</code> is bound doesn't get changed. </p>
  
  <p>If that happens, the view model property setter does raise the PropertyChanged event before it exits, which I had assumed would be enough to reset the ListBox back to the old selection. But that's not working--the ListBox still shows the new user selection. I need to override that selection and get it back in sync with the source property.</p>
  
  <p>Just in case that's not clear, here is an example: The ListBox has two items, Document1 and Document2; Document1 is selected. The user selects Document2, but Document1 fails to validate. The <code>ViewModel.CurrentDocument</code> property is still set to Document1, but the ListBox shows that Document2 is selected. I need to get the ListBox selection back to Document1.</p>
  
  <p>Here is my ListBox Binding:</p>
  
  <pre><code>&lt;ListBox 
      ItemsSource="{Binding Path=SearchResults, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
      SelectedItem="{Binding Path=CurrentDocument, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /&gt;
  </code></pre>
  
  <p>I did try using a callback from the ViewModel (as an event) to the View (which subscribes to the event), to force the SelectedItem property back to the old selection. I pass the old Document with the event, and it is the correct one (the old selection), but the ListBox selection doesn't change back.</p>
  
  <p>So, how do I get the ListBox selection back in sync with the view model property to which its <code>SelectedItem</code> property is bound? Thanks for your help.</p>