Reputation: 2029
I just started with WPF and would like some advice with the following:
I have a menu that looks like this:
<Menu Name="MenuBar"
Grid.Row="0"
HorizontalAlignment="Right"
Style="{DynamicResource MenuBarStyle}">
<MenuItem Header="Settings" />
<MenuItem Header="Help" />
</Menu>
And in my Window.Resources I have this style defined:
<Style x:Key="MenuBarStyle" TargetType="{x:Type Menu}">
<Setter Property="Background" Value="{x:Null}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontSize" Value="10" />
</Style>
This works as I expected and intended. However, I would also like to alter the Padding property of each MenuItem within my Menu. What is the best way to do this? My initial thought was to create a separate style and apply it to each MenuItem, but how would that work if my MenuItems were dynamically added (they aren't going to be, but just out of curiosity)? Is there a way I can target a child MenuItem from within my "MenuStyle" style?
Thanks,
Steve
Upvotes: 0
Views: 283
Reputation: 7560
You could use an "implicit" style (one without a x:Key-attribute) and target it to the MenuItem-type. This style is then chosen implicitly by default for all MenuItems. Put this style in Menu.Resources if you only want it to be used by this Menu, otherwise in Window.Resources where your Menu-style is.
Upvotes: 1