开发者

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:SomeGradien开发者_开发问答tPanel>

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 !


Have you tried

<MenuItem.ItemContainerStyle> 
    <Style TargetType="MenuItem" BasedOn="{StaticResource {x:Type MenuItem}}">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜