Reputation: 547
I like this solution by @CodeNaked, but somehow I'm struggling with applying it to a Menu
separator. It doesn't apply the Style
I define in my Resources
.
Here is my code:
<Menu>
<Menu.Resources>
<Style TargetType="MenuItem">
<Setter Property="Padding" Value="0,0,0,0"/>
</Style>
<Style TargetType="Separator" BasedOn="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
</Menu.Resources>
<MenuItem Header="Foo"></MenuItem>
<Separator/>
<MenuItem Header="Bar"></MenuItem>
</Menu>
If I explicitly set the Style
on the Separator
, it works.
<Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}"/>
Do you have any idea why it doesn't work in the first case?
Upvotes: 2
Views: 469
Reputation: 7325
Replace your style with:
<Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="Separator" BasedOn="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
See MSDN documentation for Separator Class:
When you create a Menu with a Separator, the control automatically applies the Style identified by the MenuItem.SeparatorStyleKey property. Styles are placed in resource dictionaries and are searched for by their keys. To change the Style of a Separator inside a Menu, you must use the MenuItem.SeparatorStyleKey property to create your new Style.
So the resource style for separator will be overridden from control and if it being put directly to the control - not.
Upvotes: 3