0

我已经创建了自定义控件,并且在该样式中菜单项不起作用我使用 BasedOn 键应用样式

通用 XAML 代码片段

<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>

该样式不适用于MenuItem,而添加如下代码的样式正在工作,如何使用基于实现这一点,因为在我的scnorio中我使用了多个菜单项

4

1 回答 1

0

你有两个选择

从样式中删除键

例如

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

这将将此样式应用于范围内的所有菜单项。

注意:如果您发现它不适合您,请尝试在菜单资源中定义样式。


或将样式直接应用于菜单项,保留样式中的键

例如

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

这会将样式仅应用于所需的菜单项


这种风格没有任何用途

<Style BasedOn="{StaticResource ResourceKey=MenuItemStyle}"  TargetType="{x:Type MenuItem}"/>
于 2014-09-21T07:35:33.690 回答