Home > DeveloperSection > Forums > How to override MenuItem Style of single role style
Tom Cruser
Tom Cruser

Total Post:28

Points:196
Posted on    November-04-2014 2:50 AM

 C# WPF  WPF Controls 
Ratings:


 1 Reply(s)
 978  View(s)
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?




Maria Susan

Total Post:29

Points:107
Posted on    November-04-2014 5:35 AM

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.


Don't want to miss updates? Please click the below button!

Follow MindStick