title: WPF ComboBox SelectedItem Set to Null on TabControl Switch
body: <p>I've got a simple problem in my WPF application which has me banging my head on the table. I have a TabControl, where every TabItem is a View generated for a ViewModel using a DataTemplate similar to this:</p>
  
  <pre><code>&lt;DataTemplate DataType="{x:Type vm:FooViewModel}"&gt;
      &lt;vw:FooView/&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>FooView contains a ComboBox:</p>
  
  <pre><code>&lt;ComboBox ItemsSource="{Binding Path=BarList}" DisplayMemberPath="Name" SelectedItem="{Binding Path=SelectedBar}"/&gt;
  </code></pre>
  
  <p>and FooViewModel contains a simple Property: <code>public Bar SelectedBar { get; set; }</code>. My problem is that when I set the value for my ComboBox, change to another tab, then change back, the ComboBox is empty again. If I set a breakpoint on the setter for my property, I see that the property is assigned to <code>null</code> when I switch to another tab.</p>
  
  <p>From what I understand, when a tab is switched, it is removed from the VisualTree - but why is it setting my ViewModel's property to <code>null</code>? This is making it very difficult for me to hold persistent state, and checking <code>value != null</code> does not seem like the right solution. Can anyone shed some like on this situation?</p>
  
  <p>Edit: The call stack at the setter breakpoint only shows [External Code] - no hints there.</p>