Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent ItemContainerStyle from overriding already set Style

Is there a way of preventing a ItemContainerStyle from overriding an already set Style (via <Style TargetType="{x:Type MenuItem}">) for instance ?

A style for a MenuItem is already defined within a ResourceDictionary XAML file, which is loaded on App startup :

<ResourceDictionary>
  <Style TargetType="{x:Type MenuItem}">
    <Setter Property="Foreground" Value="{DynamicResource TextForeground}"/>
    .. and so on
  </Style>
</ResourceDictionary>

I have the following MenuItem XAML definition. The MenuItem is wrapped inside a ContextMenu of a generic TextBlock (just worth mentioning I guess). All goes well with the menu itself, yet its children (the actual values of the Enum) get a different style, since ItemContainerStyle overrides it :

<MenuItem Header="DisplayType" 
          Name="DisplayTypeMenu"
          ItemsSource="{Binding Source={StaticResource DisplayTypeValues}}">

  <MenuItem.ItemContainerStyle>
    <Style TargetType="MenuItem">
      <Setter Property="MenuItem.IsCheckable" Value="True" />

      <Style.Triggers>
        <Trigger Property="MenuItem.Header" 
                 Value="{x:Static enums:DisplayType.Description}" >
            <Setter Property="MenuItem.IsChecked" Value="True" />
        </Trigger>
      </Style.Triggers>

    </Style>
  </MenuItem.ItemContainerStyle>

</MenuItem>

The ItemContainerStyle stems from another question of mine.

The MenuItem is placed within other layers, the top layer being a custom ContentControl :

public class SomeGradientPanel : ContentControl
{
    public SomeGradientPanel ()
    {
        DefaultStyleKey = typeof(SomeGradientPanel );
    }
}

The code above seems to be a good candidate for the source of the problem !?

Thus, the complete structure is :

<SomeNameSpace:SomeGradientPanel>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="20"></RowDefinition>
        </Grid.RowDefinitions>

        <TextBlock x:Name="SomeLabel">

          <TextBlock.ContextMenu>
              <ContextMenu>
                  <!-- The MenuItem code snippet from above !-->
              </ContextMenu>
          </TextBlock.ContextMenu>

        </TextBlock>

    </Grid>
</SomeNameSpace:SomeGradientPanel>

Can I refer to the already defined Style for the MenuItem within the ItemContainerStyle ? The Style override only occurs on the children of the said MenuItem, the parent has the expected style.

Thank you for your input !

like image 898
Dr1Ku Avatar asked Dec 13 '10 16:12

Dr1Ku


1 Answers

Have you tried

<MenuItem.ItemContainerStyle> 
    <Style TargetType="MenuItem" BasedOn="{StaticResource {x:Type MenuItem}}">
like image 141
Fredrik Hedblad Avatar answered Sep 27 '22 19:09

Fredrik Hedblad