title: WPF Merge ContextMenues in XAML
body: <p>is it possible to merge two ContextMenues in XAML?</p>
  
  <p>I created two ContextMenues as resources. I use them in a couple of DataTemplates and it works fine. However, for some DataTemplates, I would like to merge the two ContextMenues. Unfortunately, this does not seem to work.
  Here's a bit of the code from one of these ContextMenues, the other ones are defined the same:</p>
  
  <pre><code>&lt;ContextMenu x:Key="CtxIEditableViewModel" DataContext="{Binding PlacementTarget,RelativeSource={RelativeSource Self}}"&gt;
      &lt;MenuItem Header="Edit" Command="{Binding Path=DataContext.EditCommand}" CommandParameter="{Binding }"&gt;
          &lt;MenuItem.Icon&gt;
              &lt;Image Source="{StaticResource IcoEdit}"  Width="16" Height="16"&gt;&lt;/Image&gt;
          &lt;/MenuItem.Icon&gt;
      &lt;/MenuItem&gt;
      ...
  </code></pre>
  
  <p>Using one of those ContextMenues works fine:</p>
  
  <pre><code>&lt;StackPanel Orientation="Horizontal" ContextMenu="{StaticResource CtxIEditableViewModel}"&gt;
  </code></pre>
  
  <p>But how to merge two? This does not work</p>
  
  <pre><code>&lt;StackPanel Orientation="Horizontal"&gt;
          &lt;ContextMenu&gt;
              &lt;ContextMenu.ItemsSource&gt;
                  &lt;CompositeCollection&gt;
                      &lt;StaticResource ResourceKey="CtxIEditableViewModel" /&gt;
                      &lt;StaticResource ResourceKey="CtxRootViewModel" /&gt;
                  &lt;/CompositeCollection&gt;
              &lt;/ContextMenu.ItemsSource&gt;
  </code></pre>
  
  <p>And this does not work either:</p>
  
  <pre><code>&lt;StackPanel Orientation="Horizontal"&gt;
          &lt;ContextMenu&gt;
              &lt;StaticResource ResourceKey="CtxIEditableViewModel" /&gt;
              &lt;StaticResource ResourceKey="CtxRootViewModel" /&gt;
          &lt;/ContextMenu&gt;
  </code></pre>
  
  <p>When I run the programm an exception is thrown saying that the context menu may not contain a logical or visual parent. Since it works fine if I only use one ContextMenu, I do not understand the exception message. </p>
  
  <p>How can I merge those two ContextMenues in XAML (or is it not possible at all)? </p>