title: Different formatting on context menu items based on data template
body: <p>I have a working dynamic context-menu but there seems to be an issue with the XAML-formatting. To me, it seems as the first template selected will be used for all the menu items.</p>
  
  <p><a href="https://i.stack.imgur.com/7zgyq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7zgyq.png" alt="enter image description here"></a></p>
  
  <p>I would like to achieve a result where the icons are placed in the normal standard location on the left highlighted column and I could still customize the right-side XAML-content for each menu item separately as defined in their <code>DataTemplate</code>.</p>
  
  <p>Heres the XAML-code snipped, used with <code>ListBox</code></p>
  
  <pre><code>&lt;ListBox.ContextMenu&gt;
      &lt;ContextMenu ItemsSource="{Binding ModelContextMenu}"
                   ItemTemplateSelector="{StaticResource ContextMenuItemDataTemplateSelector}"&gt;
      &lt;/ContextMenu&gt;
  &lt;/ListBox.ContextMenu&gt;
  </code></pre>
  
  <p>And the data templates for different menu items:</p>
  
  <pre><code>  &lt;DataTemplate x:Key="ChangeColorMenuItem" DataType="MenuItem"&gt;
      &lt;DockPanel&gt;
        &lt;MenuItem Header="{Binding DisplayName}"
                  Command="{Binding Command}"&gt;
        &lt;/MenuItem&gt;
        &lt;xctk:ColorPicker
          SelectedColor="{Binding DataContext.SelectedColor, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:View}}}"
          Margin="0,0,0,0" /&gt;
      &lt;/DockPanel&gt;
    &lt;/DataTemplate&gt;
  
    &lt;DataTemplate x:Key="NormalMenuItem" DataType="MenuItem"&gt;
      &lt;MenuItem Header="{Binding DisplayName}"
                Command="{Binding Command}"&gt;
        &lt;MenuItem.Icon&gt;
          &lt;Image Source="{Binding Icon}"/&gt;
        &lt;/MenuItem.Icon&gt;
      &lt;/MenuItem&gt;
    &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>and finally the template selector class:</p>
  
  <pre><code>public class ContextMenuItemDataTemplateSelector : DataTemplateSelector
  {
      public override DataTemplate SelectTemplate(object item, DependencyObject container)
      {
          if (container is FrameworkElement element &amp;&amp; item is ContextMenuCommand commandItem)
          {
              switch (commandItem.MenuType)
              {
                  case ContextMenuType.Normal:
                      return
                          element.FindResource("NormalMenuItem") as DataTemplate;
                  case ContextMenuType.ChangeColor:
                      return
                          element.FindResource("ChangeColorMenuItem") as DataTemplate;
                  default:
                      throw new ArgumentOutOfRangeException();
              }
          }
  
          return null;
      }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>Each <code>MenuItem</code> content is capsulated in the list which is bind to the <code>ContextMenu</code>. The objects in the collection do contain all the data for each <code>MenuItem</code>, such as icon, display name, and <code>ICommand</code>.</p>
  
  <pre><code>public ObservableCollection&lt;ContextMenuCommand&gt; ModelContextMenu =&gt; _selectedModel.Commands;
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>The desired (standard) icon location marked with blue crosses on the following image:
  <a href="https://i.stack.imgur.com/ZtEns.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZtEns.png" alt="enter image description here"></a></p>