title: Data Binding a Collection Dependency Property to a menu in a User Control
body: <p>I have a user control with its own context menu, however I need to add additional items to that menu.</p>
  
  <p>The approach I took was to have a dependency property called ContextMenuItems:</p>
  
  <pre class="lang-vb prettyprint-override"><code>Public Shared ReadOnly ContextMenuItemsProperty As DependencyProperty = DependencyProperty.Register("ContextMenuItems", GetType(ObservableCollection(Of MenuItem)), GetType(SmartDataControl), New FrameworkPropertyMetadata(New ObservableCollection(Of MenuItem)))
  Public Property ContextMenuItems As ObservableCollection(Of MenuItem)
      Get
          Return GetValue(ContextMenuItemsProperty)
      End Get
  
      Set(ByVal value As ObservableCollection(Of MenuItem))
          SetValue(ContextMenuItemsProperty, value)
      End Set
  End Property
  </code></pre>
  
  <p>I then used a CompositeCollection to combine the static menu items from the control with the list provided by the host:</p>
  
  <pre><code>    &lt;CompositeCollection x:Key="MenuItemsCompositeCollection"&gt;
          &lt;MenuItem Header="TEST" /&gt;
          &lt;CollectionContainer Collection="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=ContextMenuItems, Converter={StaticResource TestConverter}}" /&gt;
          &lt;MenuItem Header="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=ContextMenuItems}" /&gt;
      &lt;/CompositeCollection&gt;
  </code></pre>
  
  <p>What I see when I bind to that resource is:</p>
  
  <ul>
  <li>TEST               </li>
  <li>(Collection)       </li>
  </ul>
  
  <p>The second menu item is bound to the collection to prove I can get to it. I have a test converter that I have added to menu item and it breaks in the converter method, but when I add the converter to the CollectionContainer it doesn't get called.</p>
  
  <p>Finally, I get the following error in the output window:</p>
  
  <blockquote>
    <p>System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression:Path=ContextMenuItems; DataItem=null; target element is 'CollectionContainer' (HashCode=41005040); target property is 'Collection' (type 'IEnumerable')</p>
  </blockquote>