Style from Resource not applying in menuitem

874 Views Asked by At

i have created the custom control and in that style for menu item not working i have used the BasedOn key for apply the style

Generic XAML Code snippet

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication3">

    <Style x:Key="MenuItemStyle" TargetType="{x:Type MenuItem}">
        <Setter Property="Height" Value="60"/>
        <Setter Property="Background" Value="Red"/>
    </Style>

    <Style BasedOn="{StaticResource ResourceKey=MenuItemStyle}"  TargetType="{x:Type MenuItem}"/>
    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Grid x:Name="MainGrid">
                        <Menu>
                            <MenuItem Header="File" />
                        </Menu>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

The style is not applying for MenuItem, while adding style like below code is working , how to achieve this using Based on , Since in my scnorio i have use multiple menuitems

1

There are 1 best solutions below

0
pushpraj On

you have two options here

remove the key from style

eg

<Style TargetType="{x:Type MenuItem}">
    <Setter Property="Height" Value="60"/>
    <Setter Property="Background" Value="Red"/>
</Style>

this will apply this style to all the menu items in the scope.

note: if you find it not working for you then try defining the style in Menu's resources.


or apply the style directly to menu item leaving the key in style

eg

<MenuItem Header="File" Style="{StaticResource MenuItemStyle}"/>

this will apply the style to the desired menu item only


this style does not serve any purpose

<Style BasedOn="{StaticResource ResourceKey=MenuItemStyle}"  TargetType="{x:Type MenuItem}"/>