title: MenuItem style when ItemsSource is used using setter Property with ResourceDictionaries or MergedDictionaries
body: <p>I've got a Context Menu, and is styled correctly from a ResourceDictionary that is loaded with XamlReader.Load(). The style key that I'm using is a DynamicResource that I've called styleBanner.</p>
  
  <p>In this Context Menu I have one Menu Item called Skins, which is also styled correctly with the save above dynamic resource styleBanner. But this menu-item has it's sub-menu-items data-bound to an ItemsSource in the data context View Model, and this is also working correctly.</p>
  
  <p>My trouble is that the child menu items are not styled correctly.</p>
  
  <p>Here is what is working, but not getting styled at all:</p>
  
  <pre><code>&lt;Window.ContextMenu&gt;
      &lt;ContextMenu DataContext="TimersHostViewModel" Name="TimersHostContextMenu" Style="{DynamicResource styleBanner}"&gt;
          &lt;MenuItem Name="Skins" Header="Skins" ItemsSource="{Binding Source={StaticResource TimersHostViewModel}, Path=Skins}" Style="{DynamicResource styleBanner}"&gt;
              &lt;MenuItem.ItemContainerStyle&gt;
                  &lt;Style TargetType="MenuItem"&gt;
                      &lt;Setter Property="Header" Value="{Binding Path=SkinName}"/&gt;
                      &lt;Setter Property="Command" Value="{Binding Source={StaticResource TimersHostViewModel}, Path=TimersHostContextMenuClickCommand}"/&gt;
                      &lt;Setter Property="CommandParameter" Value="{Binding Path=SkinName}"/&gt;
                  &lt;/Style&gt;
              &lt;/MenuItem.ItemContainerStyle&gt;
          &lt;/MenuItem&gt;
      &lt;/ContextMenu&gt;
  &lt;/Window.ContextMenu&gt;
  </code></pre>
  
  <p>So here is one thing that I've tried, I tried adding the following line:</p>
  
  <pre><code>&lt;Setter Property="Style" Value={DynamicResource styleBanner}"/&gt;
  </code></pre>
  
  <p>Like this:</p>
  
  <pre><code>&lt;Style TargetType="MenuItem"&gt;
      &lt;Setter Property="Header" Value="{Binding Path=SkinName}"/&gt;
      &lt;Setter Property="Style" Value="{DynamicResource styleBanner}"/&gt;
      &lt;Setter Property="Command" Value="{Binding Source={StaticResource TimersHostViewModel}, Path=TimersHostContextMenuClickCommand}"/&gt;
      &lt;Setter Property="CommandParameter" Value="{Binding Path=SkinName}"/&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>I get an exception when I try that: System.ArgumentException {"Style object is not allowed to affect the Style property of the object to which it applies."}</p>
  
  <p>So I tried this to change the added line above as follows:</p>
  
  <pre><code>&lt;Setter Property="Template" Value="{DynamicResource styleBanner}"/&gt;
  </code></pre>
  
  <p>Then I get a different exception: System.InvalidCastException {"Unable to cast object of type 'System.Windows.Style' to type 'System.Windows.FrameworkTemplate'."}</p>
  
  <p>So what is the correct way to do this? I've searched all over the internet and Stackoverflow without a clue.</p>
  
  <hr>
  
  <p>EDIT: Ok I see that the using the following now works for getting the style set on the child menu items:</p>
  
  <pre><code>&lt;Setter Property="ItemContainerStyle" Value="{DynamicResource styleBanner}"/&gt;
  </code></pre>
  
  <p>But for some reason the styleBanner background is not used for the children menu items.</p>
  
  <p>I should have posted the styleBanner from my ResourceDictionary so here it is:</p>
  
  <pre><code>&lt;!-- Banner Style --&gt;
  &lt;Style x:Key="styleBanner"&gt;
    &lt;Setter Property="StackPanel.Background"&gt;
      &lt;Setter.Value&gt;
        &lt;LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"&gt;
          &lt;GradientStop Color="DarkGray" Offset="0.1" /&gt;
          &lt;GradientStop Color="Black" Offset="1" /&gt;
        &lt;/LinearGradientBrush&gt;
      &lt;/Setter.Value&gt;
    &lt;/Setter&gt;
    &lt;Setter Property="TextBlock.Foreground" Value="White" /&gt;
    &lt;Setter Property="TextBlock.FontFamily" Value="TR2N" /&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>Here is what I have for the context menu now:</p>
  
  <pre><code>&lt;Window.ContextMenu&gt;
      &lt;ContextMenu DataContext="TimersHostViewModel" Name="TimersHostContextMenu" Style="{DynamicResource styleBanner}"&gt;
          &lt;MenuItem Name="Skins" Header="Skins" ItemsSource="{Binding Source={StaticResource TimersHostViewModel}, Path=Skins}" Style="{DynamicResource styleBanner}"&gt;
              &lt;MenuItem.ItemContainerStyle&gt;
                  &lt;Style TargetType="MenuItem"&gt;
                      &lt;Setter Property="Header" Value="{Binding Path=SkinName}"/&gt;
                      &lt;Setter Property="Command" Value="{Binding Source={StaticResource TimersHostViewModel}, Path=TimersHostContextMenuClickCommand}"/&gt;
                      &lt;Setter Property="CommandParameter" Value="{Binding Path=SkinName}"/&gt;
                      &lt;Setter Property="ItemContainerStyle" Value="{DynamicResource styleBanner}"/&gt;
                  &lt;/Style&gt;
              &lt;/MenuItem.ItemContainerStyle&gt;
          &lt;/MenuItem&gt;
      &lt;/ContextMenu&gt;
  &lt;/Window.ContextMenu&gt;
  </code></pre>
  
  <p>Here is an image showing the difference in styles that I'm getting:
  <img src="https://i.stack.imgur.com/w7vpV.jpg" alt="Difference in styles applied to different sections levels of the context menu."></p>
  
  <p>So you can see that the font &amp; font color are applied, but the styleBanner background with the gradient brush as defined in the ResourceDictionary is not.</p>
  
  <p>Actually if you look carefully I played a little trick, the styleBanner from the ResourceDictionary above was posted from a BlackSkin.xaml ResourceDictionary and it has a gradient background that should be dark grey, but the screenshot shows a blue background, because I've got a BlueSkin.xaml ResourceDictionary, but they are all similar and all have the same behavior, so here is the styleBanner from the BlueSkin.xaml ResourceDictionary, for completeness:</p>
  
  <pre><code>&lt;!-- Banner Style --&gt;
  &lt;Style x:Key="styleBanner"&gt;
    &lt;Setter Property="StackPanel.Background"&gt;
      &lt;Setter.Value&gt;
        &lt;LinearGradientBrush StartPoint="0,0.25" EndPoint="1,0.5"&gt;
          &lt;GradientStop Color="#CC0088DD" Offset="0.3" /&gt;
          &lt;GradientStop Color="#3300FFFF" Offset="0.85" /&gt;
        &lt;/LinearGradientBrush&gt;
      &lt;/Setter.Value&gt;
    &lt;/Setter&gt;
    &lt;Setter Property="TextBlock.Foreground" Value="Yellow" /&gt;
    &lt;Setter Property="TextBlock.FontFamily" Value="Comic Sans MS" /&gt;
  &lt;/Style&gt;
  </code></pre>