5

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 !

Community
  • 1
  • 1
Dr1Ku
  • 2,875
  • 3
  • 47
  • 56

1 Answers1

13

Have you tried

<MenuItem.ItemContainerStyle> 
    <Style TargetType="MenuItem" BasedOn="{StaticResource {x:Type MenuItem}}">
Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
  • Yes, but that makes the children to be displayed with an even more basic style than before (e.g. missing the Text/Header). Thank you for your input again ! – Dr1Ku Dec 13 '10 at 16:18
  • @Dr1Ku: Sure thing :) I'm failing to reproduce this. Do you have any other resources included that could cause an implicit MenuItem Style to be applied? Like PresentationFramework.Aero or something in app.xaml? – Fredrik Hedblad Dec 13 '10 at 16:32
  • @Dr1Ku: If you remove the Style for ItemContainerStyle it works fine right? – Fredrik Hedblad Dec 13 '10 at 16:38
  • I mean the style from app.xaml gets applied fine without it :) – Fredrik Hedblad Dec 13 '10 at 17:53
  • Nope, I'm home for the holidays, will have to check again at work after New Year's. Have a good one, thanks for the input ! – Dr1Ku Dec 21 '10 at 16:49
  • Hi, I've returned to the workplace, removed the ItemContainerStyle block and the display is as I would like to have it. I'll edit the question and add the ItemContainerStyle, could you take another look ? Could that be done with a DataTemplate ? – Dr1Ku Jan 10 '11 at 10:26
  • @Dr1Ku: Updated my answer, for me the BasedOn approach seems to be working. Check out my sample app and let me know if it solved it or what I should add to it in order to reproduce your problem. – Fredrik Hedblad Jan 10 '11 at 12:18
  • @Dr1Ku: Did you try my sample app? Any progress? – Fredrik Hedblad Jan 10 '11 at 23:37
  • Evening! I work part-time (Mo and We), checked at home but I can't open the solution since it's VS2010 (only have it at work :) ). The code looks solid though, should work. I really don't know why it doesn't with the code from work, as I said with the BasedOn part, the child items lose their caption/text, only the checkbox (albeit a ridiculously looking one) is visible. I'll hack on when I return Wednesday, I'll let you know, thank you for the interest !! – Dr1Ku Jan 11 '11 at 00:01
  • @Dr1Ku: Ok then :) Yes, interesting to see what the difference may be! – Fredrik Hedblad Jan 11 '11 at 00:04
  • Hi again, got assigned on other duties, I've only now remembered that this issue is still standing. I think the issue stems from using a custom ContentControl. I will update the XAML code above with more details, thank you again ! – Dr1Ku Jan 24 '11 at 12:34
  • @Dr1Ku: I tried using your updated code as well and it's still working for me. I could upload a new sample project but I don't know if it'll do much good. Anyway you can create a small project reproducing this issue and upload it and post the link here so I can look at it? – Fredrik Hedblad Jan 25 '11 at 15:53