1

I am using MetroDark theme to skin my WPF application, which unfortunately doesn't have MenuItem. So I want to change Foreground and Background for four states of menu item:

  • normal
  • hovered
  • pressed
  • disabled

So far I have code like this, and what it does is just set Foreground and Background when mouse hovering the menu item:

<Menu Background="#FF181818" Foreground="#FFABABAB" >
    <Menu.Resources>
        <Style x:Key="{x:Type MenuItem}" >
            <Style.Setters>
                <Setter Property="MenuItem.Background" Value="#FF181818" />
                <Setter Property="MenuItem.Foreground" Value="#FFABABAB" />
            </Style.Setters>
            <Style.Triggers>
                <Trigger Property="MenuItem.IsMouseOver" Value="True">
                    <Setter Property="MenuItem.Background" Value="Green" />
                    <Setter Property="MenuItem.Foreground" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Menu.Resources>
    <MenuItem Header="File">
        <MenuItem Header="New" Command="ApplicationCommands.New"></MenuItem>
        <MenuItem Header="Open" Command="ApplicationCommands.Open"></MenuItem>
        <MenuItem Header="Save" Command="ApplicationCommands.Save"></MenuItem>
        <MenuItem Header="Save As" Command="ApplicationCommands.SaveAs"></MenuItem>
        <Separator Background="#FF181818" Foreground="#FFABABAB"></Separator>
        <MenuItem Header="Delete" Command="ApplicationCommands.Delete"></MenuItem>
        <MenuItem Header="Quit" Click="Quit_MenuItem_Click"></MenuItem>
    </MenuItem>
    <MenuItem Header="Format" Click="Format_MenuItem_Click" ></MenuItem>
    <MenuItem Header="About" ></MenuItem>
</Menu>

The result is: The setting of Foreground works, however, that for Background only works for the MenuItem which are immediate children of Menu, not whose sub-menu items.

enter image description here

enter image description here

I have noticed this Styled MenuItem on WPF , which may be relevant. But my question is can I fix this without ControlTemplate? And I am looking for a solution can be applied for all the other states, like IsDisabled and IsPressed.

Thanks so much!

Community
  • 1
  • 1
VincentZHANG
  • 757
  • 1
  • 13
  • 31

1 Answers1

0

You will need modify the template. Very easy to do within Blend. I would suggest creating a SimpleMenuItem in Blend and edit a copy of that template. They are a bit easier to work with then the default templates imo.

poco
  • 2,935
  • 6
  • 37
  • 54