title: WPF combobox not displaying correct value after updating binding source
body: <p>I'm using a WPF combobox and I've noticed a problem where the combobox is not showing the correct bound value after updating the binding source while the source itself is in the process of being updated from the binding.</p>
  
  <p>I've put together a simple example to demonstrate this. In this example, I've got a combobox containing 4 items (the strings "A","B","C" and "D"). There is a two-way binding between <strong>SelectedItem</strong> on the combobox to a property on the datacontext called <strong>ComboSelectedItem</strong>.</p>
  
  <p>The intended functionality is that if the user selects "A","B" or "C" from the combobox, then the logic  in the datacontext will attempt to reset the selection on the combobox to "D".
  However, instead what happens is that if the user selects "A" from the combobox, the selection remains on "A".</p>
  
  <p>Here's the sample code below:</p>
  
  <p>MainWindow.xaml:</p>
  
  <pre><code>&lt;Window x:Class="Testing.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}"&gt;
  &lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="Auto" /&gt;
          &lt;RowDefinition Height="Auto" /&gt;
          &lt;RowDefinition Height="Auto" /&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition Width="Auto" /&gt;
          &lt;ColumnDefinition Width="Auto" /&gt;
      &lt;/Grid.ColumnDefinitions&gt;
  
      &lt;Label Grid.Row="0" Grid.Column="0" Margin="10,10,10,10"&gt;Combobox test:&lt;/Label&gt;
      &lt;ComboBox Grid.Row="0" Grid.Column="1" Margin="10,10,10,10" x:Name="comboBox"
                ItemsSource="{Binding Path=ComboBoxItems}" Width="80"
                SelectedItem="{Binding Path=ComboSelectedItem, Mode=TwoWay}"/&gt;
  
          &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>and the code-behind for it:</p>
  
  <pre><code>using System;
  using System.Windows;
  using System.Collections.ObjectModel;
  using System.ComponentModel;
  using System.Threading;
  using System.Windows.Threading;
  
  namespace Testing
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window, INotifyPropertyChanged
      {
          public MainWindow()
          {
              InitializeComponent();
          }
  
          private ObservableCollection&lt;String&gt; items;
          public ObservableCollection&lt;String&gt; ComboBoxItems
          {
              get
              {
                  if (items == null)
                  {
                      items = new ObservableCollection&lt;string&gt;();
                      items.Add("A");
                      items.Add("B");
                      items.Add("C");
                      items.Add("D");
                  }
  
                  return items;
              }
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          private string comboSelectedItem;
          public string ComboSelectedItem
          {
              get { return comboSelectedItem; }
              set
              {
                  comboSelectedItem = value;
  
                  if (PropertyChanged != null)
                      PropertyChanged(this, new PropertyChangedEventArgs("ComboSelectedItem"));
  
                  //if value != D, set to D
                  if (ComboSelectedItem != "D")
                  {
                       ComboSelectedItem = "D";
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>I've found that if I queue up the <strong>ComboSelectedItem</strong> set so that it happens on the UI thread, then this will work e.g.</p>
  
  <pre><code>public string ComboSelectedItem
      {
          get { return comboSelectedItem; }
          set
          {
              comboSelectedItem = value;
  
              if (PropertyChanged != null)
                  PropertyChanged(this, new PropertyChangedEventArgs("ComboSelectedItem"));
  
              //if value != D, set to D
              if (ComboSelectedItem != "D")
              {
                  ThreadPool.QueueUserWorkItem(delegate(Object theElement)
                  {
                      UIElement elem = (UIElement)theElement;
                      elem.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate()
                          {
                              ComboSelectedItem = "D";
                          });
                  }, comboBox);
              }
          }
      }
  </code></pre>
  
  <p>However, I'm not completely sure why this works and anyway I would prefer not to have to do this for all the comboboxes in my application where a scenario like this may occur.</p>
  
  <p>Instead, is there a setting/property on the Combobox or some other method that would solve this issue for me? Thanks.</p>