DermFrench
DermFrench

Reputation: 4057

WPF MenuItem Foreground is setting to child menus - only want it on main menu

I have a WPF menu which I set the foreground colour to a binding (using a converter), say the Foreground of my top level menu goes to Green for example, all the child menus under it go green (which I do not want).

Code sample: I want the Finishing Position top menu to go green or whatever the converter tells it do. However I do not want the sub MenuItemDeleteSelection to go to that same colour I want it just to be black or the default greyed out when it's command binding is Can Execute= false).

<Menu>
    <MenuItem Header="{Binding FinishingPosition,Converter={StaticResource FinishingPositionToDisplayTextConverter1}}"  Height="17" Width="12" Padding="0" Name="SelectionStatusHeader" Foreground="{Binding FinishingPosition,Converter={StaticResource FinishingPositionToColourConverter1}}" Background="White" HorizontalContentAlignment="Center">
        <MenuItem Name="MenuItemDeleteSelection" Header="Delete Selection"
                    Command="{Binding Path=DataContext.DeleteSelectionCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" 
                    CommandParameter="{Binding}" />

Upvotes: 4

Views: 1092

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81243

Simply have default style for MenuItem in your Menu resources so that it gets applied automatically to all your menu items.

Set default value to be SystemColors.MenuTextBrushKey which will get applied to all menu items except for the one which overrides it.

<Menu>
   <Menu.Resources>
       <Style TargetType="MenuItem">
           <Setter Property="Foreground"
              Value="{StaticResource {x:Static SystemColors.MenuTextBrushKey}}"/>
       </Style>
    </Menu.Resources>
    <MenuItem Foreground="Green"/>
    <MenuItem/>
    <MenuItem/>
</Menu>

Upvotes: 4

Related Questions