title: ComboBox options/items from CollectionViewSource on parent control
body: <p>Working on a CRM system where I may have multiple orders open at once, and every order uses the same options for an order number prefix, which is a string.  I would like for all order instances, which are instantiated in viewmodel classes bound to instances of a custom UserControl, to consult a single CollectionViewSource on a parent control, so I only need one place for that common data.</p>
  
  <p>The XAML for the parent control includes:</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;CollectionViewSource x:Key="vsOrderPrefixes" /&gt;
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>Its code-behind loads the CollectionViewSource with a collection of Tuples where Item1 is the drop-down option and Item2 determines sort order:</p>
  
  <pre><code>private void LoadCommonOrderOptions()
  {
      var vsOrderPrefixes = ((CollectionViewSource)(FindResource("vsOrderPrefixes")));
      var prefixes = new ObservableCollection&lt;Tuple&lt;string, int&gt;&gt;(
          DataRepository.Instance.GetOptionsByCodeName(Constants.OrderPrefixCodeName));
      vsOrderPrefixes.Source = prefixes.OrderBy(pr =&gt; pr.Item2);
  }
  </code></pre>
  
  <p>The ComboBox in the child control XAML, its syntax partially informed by another post:</p>
  
  <pre><code>&lt;ComboBox x:Name="cbxOrderPrefix" SelectedValue="{Binding OrderPrefix}"
      DataContext="{DynamicResource vsOrderPrefixes}" ItemsSource="{Binding Path=.}"
      DisplayMemberPath="Item1" SelectedValuePath="Item1" /&gt;
  </code></pre>
  
  <p>Nothing special about the viewmodel's bound property:</p>
  
  <pre><code>private string _orderPrefix;
  
  public string OrderPrefix
  {
      get { return _orderPrefix; }
      set
      {
          if (_orderPrefix != value)
          {
              _orderPrefix = value;
              NotifyListeners();  // Fires PropertyChanged event
          }
      }
  }
  </code></pre>
  
  <p>Not sure how it matters, but inside a function of one of the child control's ancestors ("UserControl" in paragraph below), I create the VM, the child control, and a TabItem the child will go on.  Then I assign the VM to the child's DataContext, make the child the TabItem's Content, then add the TabItem to the ancestor's TabControl:</p>
  
  <pre><code>var tabb = new TabItem() { Title = orderVM.OrderID };
  var uclOrder = new OrderInfo();    // Child control
  uclOrder.DataContext = orderVM;
  tabb.Content = uclOrder;
  tclOrders.Items.Add(tabb);
  </code></pre>
  
  <p>Finally, if it matters, the child control isn't <i>directly</i> on the parent control, it's more like Parent | TabControl | TabItem | UserControl | TabControl | TabItem | ScrollViewer | TabControl | TabItem | Child.</p>
  
  <p><b>THE GOOD:</b> the ComboBox does in fact populate with the retrieved options, observable by revealing the drop-down.</p>
  
  <p><b>THE BAD:</b> selecting one of the options leaves the ComboBox blank, and the bound property set is never reached.</p>
  
  <p><b>THE WEIRD:</b> upon load, the ComboBox appears to have the first option selected, and then if you keep pressing the "down" arrow key, beginning with the second press and every third press thereafter, the first option appears selected again, and is blank in between.  Again, no property set.  And pressing the "up" arrow key leaves it blank every time.</p>
  
  <p>What am I doing wrong, please?  Thanks...</p>