title: WPF MenuItem style is not applied in TextBox context menu
body: <p>I once created a style to fix the bugged appearance of menu items in WPF. It's mainly about the menu text that is misaligned. It's too far in the top left corner and doesn't use proper spacing.</p>
  
  <p>I discovered that it does work in window menus, but <strong>not</strong> in the context menu of <code>TextBox</code> which I tested now. So the question is, why isn't this style regarded for context menus from a text box?</p>
  
  <p><strong>Update:</strong> I found out that the <code>TextBox</code> uses its own menu item class, a private nested class <code>TextEditorContextMenu.EditorContextMenu</code> and its own menu items, the nested class <code>EditorMenuItem</code>. Both are derived from <code>ContextMenu</code> and <code>MenuItem</code>, resp. So if they are a subclass of the classes I have styled, then why isn't my style applied to them as well?</p>
  
  <p>The only thing I could do was copying the definition of</p>
  
  <pre><code>&lt;ControlTemplate x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type MenuItem}, ResourceId=SubmenuItemTemplateKey}" TargetType="{x:Type MenuItem}"&gt;
  </code></pre>
  
  <p>from PresentationFramework.Aero's resources into my style file. But that very clearly makes my menus look like Windows 7 which may not be expected on Windows 8 or 10 (or XP). But redefining that style key at lest could influence the <code>EditorMenuItem</code>'s appearance. Why?</p>
  
  <p>And if <code>EditorMenuItem</code> doesn't have its own style (I couldn't find it), why doesn't it use whatever style I provide for the base class as well? How does it know <em>not</em> to use my style but only the default one which is replaced and inaccessible for every other context menu?</p>
  
  <p>Here's the XAML code, which is stored in MenuStyles.xaml and included in the <code>ResourceDictionary</code> from App.xaml.</p>
  
  <pre><code>&lt;ResourceDictionary
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
  
      &lt;!-- Expression Blend 4 created this (and a lot more) from some system theme on Windows 7 --&gt;
      &lt;Style TargetType="{x:Type MenuItem}"&gt;
          &lt;Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/&gt;
          &lt;Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/&gt;
          &lt;Setter Property="Background" Value="Transparent"/&gt;
          &lt;Setter Property="ScrollViewer.PanningMode" Value="Both"/&gt;
          &lt;Setter Property="Stylus.IsFlicksEnabled" Value="False"/&gt;
          &lt;Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/&gt;
          &lt;Style.Triggers&gt;
              &lt;Trigger Property="Role" Value="TopLevelHeader"&gt;
                  &lt;Setter Property="Padding" Value="7,2,8,3"/&gt;
                  &lt;Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/&gt;
              &lt;/Trigger&gt;
              &lt;Trigger Property="Role" Value="TopLevelItem"&gt;
                  &lt;Setter Property="Padding" Value="7,2,8,3"/&gt;
                  &lt;Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/&gt;
              &lt;/Trigger&gt;
              &lt;Trigger Property="Role" Value="SubmenuHeader"&gt;
                  &lt;Setter Property="Padding" Value="5,4,2,3"/&gt;
                  &lt;!-- Changed from 2,3,2,3 --&gt;
                  &lt;Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/&gt;
              &lt;/Trigger&gt;
              &lt;Trigger Property="Role" Value="SubmenuItem"&gt;
                  &lt;Setter Property="Padding" Value="5,4,2,3"/&gt;
                  &lt;!-- Changed from 2,3,2,3 --&gt;
              &lt;/Trigger&gt;
          &lt;/Style.Triggers&gt;
      &lt;/Style&gt;
  
      &lt;!-- Expression Blend 4 created this from some system theme on Windows 7 --&gt;
      &lt;!-- Edited like in: http://devlicio.us/blogs/christopher_bennage/archive/2008/06/19/styling-separators-in-wpf.aspx --&gt;
      &lt;!-- Decreased in height to be more platform standard --&gt;
      &lt;Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}"&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type Separator}"&gt;
                      &lt;Grid Margin="0,3,0,2" SnapsToDevicePixels="true"&gt;
                          &lt;!-- Changed from 0,6,0,4 --&gt;
                          &lt;Rectangle Fill="#E0E0E0" Height="1" Margin="30,0,1,1"/&gt;
                          &lt;Rectangle Fill="White" Height="1" Margin="30,1,1,0"/&gt;
                      &lt;/Grid&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>