title: WPF ComboBox SelectedItem
body: <p>Ok been working with WPF for a while but I need some help.</p>
  
  <p>I have a <code>ComboBox</code> like below:</p>
  
  <pre><code>&lt;TabControl&gt;
      &lt;TabItem Header="1"&gt;
          &lt;ComboBox ItemsSource="{Binding MyList}" SelectedItem="{Binding MyListSelection}"/&gt;
      &lt;/TabItem&gt;
      &lt;TabItem Header="2"/&gt;
  &lt;/TabControl&gt;
  </code></pre>
  
  <p>Whenever I move away from tab 1 and then come back to it the selection gets removed. I think the reason for that is that the controls get destroyed when they go out of scope and then back in. But in the process of that the SelectedItem becomes null which isn't really what the user wanted, it's an event due to the UI lifecycle.</p>
  
  <p>So I'm wondering what is the best route to take? I'm building this app with MVVM so I could ignore a set call on the MyListSelection Property in my ViewModel but I have ComboBoxes all over the place and don't like modifying my ViewModel for what I consider a bug of WPF. </p>
  
  <p>I could subclass the WPF ComboBox, but there is no SelectedItemChanging event I can only add a handler when SelectedItem changed.</p>
  
  <p>Any ideas?</p>
  
  <p>UPDATE:</p>
  
  <p>Okay, after beating my head against the wall I found out why my problem couldn't get reproduced. If the list item type is a class for some reason the SelectedItem gets set by WPF to null but if it's a value type it doesn't.</p>
  
  <p>here's my test class(VMBase is just an abstract class that implements INotifyPropertyChanged):</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class TestListViewModel : VMBase
  {
      public TestListViewModel()
      {
          TestList = new List&lt;TestViewModel&gt;();
          for (int i = 0; i &lt; 10; i++)
          {
              TestList.Add(new TestViewModel(i.ToString()));
          }
      }
  
      public List&lt;TestViewModel&gt; TestList { get; set; }
  
      TestViewModel _SelectedTest;
      public TestViewModel SelectedTest
      {
          get { return _SelectedTest; }
          set
          {
              _SelectedTest = value;
              OnPropertyChanged("SelectedTest");
          }
      }
  }
  
  public class TestViewModel : VMBase
  {
    public string Name {get;set;}
  }
  </code></pre>
  
  <p>So when I change TestList to type int and go back and forth between tabs SelectedItem stays the same. But when it is of type <code>TestViewModel</code> SelectedTest gets set to null when the tabitem goes out of focus.</p>
  
  <p>What's going on?</p>