title: Context Menu inheritance in WPF
body: <p>I have TreeView which contains different item types. Item Styles are defined over a custom ItemContainerStyleSelector property.  </p>
  
  <p>My styles are all sharing a base style and only item specific stuff is defined in each style. It looks like this:</p>
  
  <pre><code>&lt;Style x:Key="BaseStyle" TargetType="{x:Type TreeViewItem}"&gt;
  ...
  &lt;/Style&gt;
  
  &lt;Style x:Key ="SomeSpecificStyle" TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource BaseStyle}"&gt;
     &lt;Setter Property="ContextMenu" Value="{StaticResource NodeContextMenu}"/&gt;
     ...  
  &lt;/Style&gt;
  
  &lt;Style x:Key ="SomeSpecificStyle" TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource BaseStyle}"&gt;
     &lt;Setter Property="ContextMenu" Value="{StaticResource AnotherNodeContextMenu}"/&gt;
     ...  
  &lt;/Style&gt;
  </code></pre>
  
  <p>The context menu is defined like this</p>
  
  <pre><code>&lt;ContextMenu x:Key="NodeContextMenu"&gt;
    &lt;MenuItem Header="Select Views" Command="{Binding Path=OpenViewsCommand}" /&gt;
    ...other specific entries
    &lt;MenuItem Header="Remove" Command="{Binding Path=DocumentRemoveCommand}" /&gt;
    ...other entries common for all menus
  &lt;/ContextMenu&gt;
  </code></pre>
  
  <p>Another context menu also should contain those common items like remove. These need to be replicated by copy paste every time the command properties, etc are changing. A hell for maintainability. Is there a way to define a context menu which contains the common items, and then "derive" specific context menus?</p>
  
  <p>Edit: I found a solution with the hints from this thread:
  I define a Collection with the common items, and use a composite collection when defining a menu to include both new items and the common items collection</p>
  
  <pre><code>&lt;CompositeCollection x:Key="CommonItems"&gt; 
    &lt;MenuItem Header="Remove" Command="{Binding Path=DocumentRemoveCommand}"&gt;
    ....Other common stuff
  &lt;/CompositeCollection&gt;
  
  &lt;ContextMenu x:Key="NodeContextMenu"&gt;
    &lt;ContextMenu.ItemsSource&gt;
      &lt;CompositeCollection&gt;
        &lt;MenuItem Header="Select Views" Command="{Binding Path=OpenViewsCommand}" /&gt;
        &lt;CollectionContainer Collection="{StaticResource CommonItems}" /&gt;
      &lt;/CompositeCollection&gt;
    &lt;/ContextMenu.ItemsSource&gt;
  &lt;/ContextMenu&gt;
  </code></pre>