title: Databinding to ObservableCollection in a different UserControl - how to preserve current selections?
body: <p><strong>Scope of question expanded on 2010-03-25</strong></p>
  
  <p>I ended up figuring out my problem, but here's a new problem that came up as a result of solving the original question, because I want to be able to award the bounty to someone!!!</p>
  
  <p>Once I figured out my problem, I soon found out that when the ObservableCollection updates, the databound ComboBox has its contents repopulated, but <em>most</em> of the selections have been blanked out.</p>
  
  <p>I assume that in this case, MVVM is going to make it difficult for me to remember the last selected item.  I have an idea, but it seems a little nasty.  I'll award the bounty to whomever comes up with a nice solution for this!</p>
  
  <p><strong>Question re-written on 2010-03-24</strong></p>
  
  <p>I have two UserControls, where one is a dialog that has a TabControl, and the other is one that appears within said TabControl.  I'll just call them <strong>CandyDialog</strong> and <strong>CandyNameViewer</strong> for simplicity's sake.  There's also a data management class called <strong>Tracker</strong> that manages information storage, which for all intents and purposes just exposes a public property that is an ObservableCollection.</p>
  
  <p>I display the CandyNameViewer in CandyDialog via code behind, like this:</p>
  
  <pre><code>private void CandyDialog_Loaded( object sender, RoutedEventArgs e)
  {
    _candyviewer = new CandyViewer();
    _candyviewer.DataContext = _tracker;
    candy_tab.Content = _candyviewer;
  }
  </code></pre>
  
  <p>The CandyViewer's XAML looks like this (edited for kaxaml):</p>
  
  <pre><code>&lt;Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
      &lt;Page.Resources&gt;
          &lt;DataTemplate x:Key="CandyItemTemplate"&gt;
              &lt;Grid&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="120"&gt;&lt;/ColumnDefinition&gt;
                      &lt;ColumnDefinition Width="150"&gt;&lt;/ColumnDefinition&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;TextBox Grid.Column="0" Text="{Binding CandyName}" Margin="3"&gt;&lt;/TextBox&gt;
                  &lt;!-- just binding to DataContext ends up using InventoryItem as parent, so we need to get to the UserControl --&gt;
                  &lt;ComboBox Grid.Column="1" SelectedItem="{Binding SelectedCandy, Mode=TwoWay}" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.CandyNames}" Margin="3"&gt;&lt;/ComboBox&gt;
              &lt;/Grid&gt;
          &lt;/DataTemplate&gt;
      &lt;/Page.Resources&gt;
  
      &lt;Grid&gt;
          &lt;ListBox DockPanel.Dock="Top" ItemsSource="{Binding CandyBoxContents, Mode=TwoWay}" ItemTemplate="{StaticResource CandyItemTemplate}" /&gt;
      &lt;/Grid&gt;
  &lt;/Page&gt;
  </code></pre>
  
  <p>Now everything works fine when the controls are loaded.  As long as CandyNames is populated <strong>first</strong>, and then the consumer UserControl is displayed, all of the names are there.  I obviously don't get any errors in the Output Window or anything like that.</p>
  
  <p><em>The issue I have is that when the ObservableCollection is modified from the model, those changes are <strong>not</strong> reflected in the consumer UserControl!  I've never had this problem before;  all of my previous uses of ObservableCollection updated fine, although in those cases I wasn't databinding across assemblies.  Although I am currently only adding and removing candy names to/from the ObservableCollection, at a later date I will likely also allow renaming from the model side.</em>  </p>
  
  <p>Is there something I did wrong?  Is there a good way to actually debug this?  <a href="https://stackoverflow.com/questions/1798600/mvvm-what-is-the-ideal-way-for-usercontrols-to-talk-to-each-other">Reed Copsey indicates here</a> that inter-UserControl databinding is possible.  Unfortunately, my favorite <a href="http://bea.stollnitz.com/blog/?p=52" rel="nofollow noreferrer">Bea Stollnitz article on WPF databinding debugging</a> doesn't suggest anything that I could use for this particular problem.</p>