title: How do I fix SelectedItem null in listBox when using Task issue
body: <p>I have a problem with something setting up selectedItem in a listbox to null. But it does it every other time, and I can’ figure out why it happens. It only happens when I’m trying to use Task to get some data async. I created a very simple project to show the exact problem, which I find difficult to understand. 
  So here is my xaml </p>
  
  <pre><code>&lt;Window x:Class="AsyncProblem.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
      &lt;StackPanel Orientation="Horizontal"&gt;
         &lt;ListBox ItemsSource="{Binding Names}" SelectedItem="{Binding SelectedName}" /&gt;
          &lt;Button Click="Button_Click" Content="Reload" Width="50" Height="50" /&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Here is my viewmodel: </p>
  
  <pre><code>namespace AsyncProblem
  {
  public class MainWindowViewModel : NotifyPropertyChanged
  {
      public MainWindowViewModel()
      {
          Names = new ObservableCollection&lt;string&gt;() { "Name", "Second Name",  "Third Name"};
      }
  
      private ObservableCollection&lt;string&gt; names;
      public ObservableCollection&lt;string&gt; Names
      {
          get { return names; }
          set
          {
              names = value;
              OnPropertyChanged();
          }
      }
  
      private string selectedName;
      public string SelectedName
      {
          get { return selectedName; }
          set
          {
              selectedName = value;
              OnPropertyChanged();
          }
      }
  
      Task loadTask;
      int counter = 0;
      public void LoadNewList()
      {
          counter++;
          loadTask = Task.Factory.StartNew(() =&gt;
              {
                  Thread.Sleep(2000);
                  Names = new ObservableCollection&lt;string&gt;()
                  {
                      string.Format("{0} new name", counter), 
                      string.Format("{0} second new name", counter),
                      string.Format("{0} third new name", counter)
                  };
              });
      }
      public void ReselectSelectedName()
      {
          loadTask.ContinueWith(previousTask =&gt; SelectedName = Names.LastOrDefault());
      }
      }
  }
  </code></pre>
  
  <p>And here is the calling code</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      private MainWindowViewModel dataContext;
      public MainWindow()
      {
          InitializeComponent();
          dataContext = new MainWindowViewModel();
          DataContext = dataContext;
      }
  
      private void Button_Click(object sender, RoutedEventArgs e)
      {
          dataContext.LoadNewList();
          dataContext.ReselectSelectedName();
      }
  }
  </code></pre>
  
  <p>Now when I run this program and press the button, if gets the new list, but it does not set the selectedName. In fact what happens, the selected name changes to iza, and then somehow the selectedname set is called second time with a value of null. So of coursethe listbox loses its selected item.
  Even worse is that, it only happens every second button click, when I try to hit the button again then I get the desired behaviour and again the behaviour gets wrong and so on.</p>
  
  <p>Can anybody explain what happens behind the scene?</p>
  
  <p>Thank you</p>