title: Dynamic and static MenuItems, both in MainMenu and ContextMenu
body: <p>I whould like to have a menu like the following:</p>
  
  <pre><code>┌──────────────────────────┐
  │ MenuItem always the same │
  │ &lt;Separator /&gt;            │
  │ MenuItem read from XML 1 │
  │ MenuItem read from XML 2 │
  │ MenuItem read from XML n │
  └──────────────────────────┘
  </code></pre>
  
  <p>And this should be reused in a MainMenu and as a submenu of a ContextMenu also.</p>
  
  <p>I currently have the following in the XAML:</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;XmlDataProvider x:Key="ItemTypes" Source="C:\Config.xml"
          XPath="Configuration/ItemTypes/ItemType" /&gt;
  
      &lt;collections:ArrayList x:Key="mnuAdd" x:Shared="False"&gt;
          &lt;MenuItem Command="local:MainWindow.AddItemGroup" Header="Item Group" /&gt;
          &lt;Separator /&gt;
          &lt;ItemsControl ItemsSource="{Binding Source={StaticResource ItemTypes}}" /&gt;
      &lt;/collections:ArrayList&gt;
  
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>And have the following in the XML:</p>
  
  <pre><code>&lt;Configuration&gt;
      &lt;ItemTypes&gt;
          &lt;ItemTypeName="Item1" DisplayName="First Item" /&gt;
          &lt;ItemTypeName="Item2" DisplayName="Second Item" /&gt;
      &lt;/ItemTypes&gt;
  &lt;/Configuration&gt;
  </code></pre>
  
  <p>And of course I have HierarchicalDataTemplate, but it never displays right.</p>
  
  <p>If I set it to this one:</p>
  
  <pre><code>    &lt;HierarchicalDataTemplate DataType="ItemType"&gt;
          &lt;TextBlock Text="{Binding XPath=@DisplayName, StringFormat={}{0}}"
              Tag="{Binding XPath=@Name}" MouseLeftButtonUp="AddItemMenu_Click"
              MouseRightButtonUp="AddItemMenu_Click" /&gt;
      &lt;/HierarchicalDataTemplate&gt;
  </code></pre>
  
  <p>It displays this way:</p>
  
  <p><img src="https://i.stack.imgur.com/t2GFM.png" alt="enter image description here"></p>
  
  <p>If I set it to this one:</p>
  
  <pre><code>    &lt;HierarchicalDataTemplate DataType="ItemType"&gt;
          &lt;MenuItem Header="{Binding XPath=@DisplayName, StringFormat={}{0}}"
              Tag="{Binding XPath=@Name}" MouseLeftButtonUp="AddItemMenu_Click" 
              MouseRightButtonUp="AddItemMenu_Click" /&gt;
      &lt;/HierarchicalDataTemplate&gt;
  </code></pre>
  
  <p>It displays this way:</p>
  
  <p><img src="https://i.stack.imgur.com/GnSfO.png" alt="enter image description here"></p>
  
  <p><strong>How can I style it to display correctly as normal menu items?</strong></p>
  
  <p><em>Please not that according to previous research, having <code>&lt;collections:ArrayList x:Key="mnuAdd" x:Shared="False"&gt;</code> is mandatory as I want the same static resource to be displayed both in MainMenu and in ContextMenu. If I don't use this, one of them disappears.</em></p>
  
  <p><strong>UPDATE 1:</strong></p>
  
  <p>According to H.B. changed the <code>&lt;collections:ArrayList&gt;</code> to <code>CompositeCollection</code>:</p>
  
  <pre><code>    &lt;CompositeCollection x:Key="mnuAdd" x:Shared="False"&gt;
          &lt;MenuItem Command="local:MainWindow.AddItemGroup" Header="Item Group" /&gt;
          &lt;Separator /&gt;
          &lt;CollectionContainer Collection="{Binding Source={StaticResource ItemTypes}}" /&gt;
      &lt;/CompositeCollection&gt;
  </code></pre>
  
  <p>Now if my DataTemplate is <code>MenuItem</code>, it still displays awkward:</p>
  
  <p><img src="https://i.stack.imgur.com/vcU0K.png" alt="enter image description here"></p>
  
  <p>If my DataTemplate is <code>TextBlock</code>, it displays fine, but the clicks are only handled if it is <em>on the text</em>, not if it is a little left from the text.</p>
  
  <p><img src="https://i.stack.imgur.com/sVLG1.png" alt="enter image description here"></p>
  
  <p><strong>How can I make it work good and look good?</strong></p>
  
  <p><strong>Edit 2:</strong>
  Managed to solved the click issue by extending the <code>TextBlock</code> horizontally by the following:</p>
  
  <pre><code>    &lt;DataTemplate DataType="ItemType"&gt;
          &lt;TextBlock Text="{Binding XPath=@DisplayName, StringFormat={}{0}}"
              Tag="{Binding XPath=@Name}" MouseLeftButtonUp="AddItemMenu_Click" 
              MouseRightButtonUp="AddTestItemMenu_Click"
              Margin="-30,0,-60,0" Padding="30,0,60,0" HorizontalAlignment="Stretch" /&gt;
      &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>Now it looks good and works well.</p>