HOW TO OVERRIDE MENUITEM STYLE OF SINGLE ROLE STYLE

Tom Cruser

Total Post:28

Points:196
Posted by  Tom Cruser
C#
 1477  View(s)
Ratings:
Rate this:
is there any way to override a single role style of MenuItem? I know the way to override the IsHighlighted color of MenuItem is to override the ContentTemplate. What i want is to override the ContentTemplate for Role "SubmenuItem"

<Style x:Key="ActionMenuItemStyle" TargetType="{x:Type MenuItem}">

    <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />

    <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />

    <Setter Property="Background" Value="Transparent" />

    <Setter Property="Template" Value="{StaticResource SubmenuItemTemplateKey2}" />

    <Setter Property="Foreground" Value="Black" />

    <Style.Triggers>

        <Trigger Property="Role" Value="TopLevelHeader">

            <Setter Property="Padding" Value="7,2,8,3" />

            <Setter Property="Template" Value="???" />

            <Setter Property="Foreground" Value="White" />

        </Trigger>

        <Trigger Property="Role" Value="TopLevelItem">

            <Setter Property="Padding" Value="7,2,8,3" />

            <Setter Property="Template" Value="???" />

        </Trigger>

        <Trigger Property="Role" Value="SubmenuHeader">

            <Setter Property="Padding" Value="2,3,2,3" />

            <Setter Property="Template" Value="???" />

        </Trigger>

        <Trigger Property="Role" Value="SubmenuItem">

            <Setter Property="Padding" Value="2,3,2,3" />

        </Trigger>

    </Style.Triggers>

</Style>

The {StaticResource SubmenuItemTemplateKey2} is my override ContentTemplate. For the other roles i want use the default templates of MenuItem. Is there any way to do it?


  1. Maria Susan

    Post:29

    Points:107
    Re: How to override MenuItem Style of single role style

    You're trying to override all the menu items and next trying to override it again (to set it back to default style). In this case you just need to override the menu item with role of SubmenuItem, so the code can be just like this:

    <Style x:Key="ActionMenuItemStyle" TargetType="{x:Type MenuItem}">
      <Setter Property="HorizontalContentAlignment" 
             Value="{Binding Path=HorizontalContentAlignment, 
             RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
      <Setter Property="VerticalContentAlignment" 
             Value="{Binding Path=VerticalContentAlignment, 
             RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
      <Setter Property="Background" Value="Transparent" />
      <Setter Property="Foreground" Value="Black" />
      <Style.Triggers>
        <Trigger Property="Role" Value="TopLevelHeader">
            <Setter Property="Padding" Value="7,2,8,3" />
            <Setter Property="Foreground" Value="White" />
        </Trigger>
        <Trigger Property="Role" Value="TopLevelItem">
            <Setter Property="Padding" Value="7,2,8,3" />
        </Trigger>
        <Trigger Property="Role" Value="SubmenuHeader">
            <Setter Property="Padding" Value="2,3,2,3" />
        </Trigger>
        <Trigger Property="Role" Value="SubmenuItem">
            <Setter Property="Padding" Value="2,3,2,3"/>
            <!-- override here -->
            <Setter Property="Template" 
                    Value="{StaticResource SubmenuItemTemplateKey2}"/>
        </Trigger>
      </Style.Triggers>
    </Style>

    Otherwise (following your original approach), we may need some dummy MenuItem element which has the default style. Then we can Bind the Template of whatever items to the Template of that dummy element to restore their default style.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!