我有一个 MenuItem 样式:
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"
Height="22" SnapsToDevicePixels="true">
<Grid Margin="-1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="4"/>
<ColumnDefinition SharedSizeGroup="MenuItemIGTColumnGroup" Width="Auto"/>
</Grid.ColumnDefinitions>
<ContentPresenter x:Name="menuHeaderContainer" Grid.Column="0"
ContentSource="Header" HorizontalAlignment="Left"
Margin="{TemplateBinding Padding}" RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="Center"/>
<TextBlock x:Name="menuGestureText" Grid.Column="2"
Margin="{TemplateBinding Padding}" Opacity="0.7" Text="{TemplateBinding InputGestureText}"
VerticalAlignment="Center"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsHighlighted" Value="True">
<Setter Property="Background" TargetName="templateRoot" Value="Black"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsHighlighted" Value="True"/>
<Condition Property="IsEnabled" Value="False"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="templateRoot" Value="Red"/>
<Setter Property="BorderBrush" TargetName="templateRoot" Value="Black"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
我的控制是:
<Grid>
<Menu VerticalAlignment="Center" HorizontalAlignment="Center">
<MenuItem Header="Header">
<MenuItem Header="Second Level"/>
</MenuItem>
</Menu>
当鼠标悬停在菜单项上时,我愿意设置新样式,背景将是黑色的。我的风格有效,但只有二级菜单项。
当鼠标悬停在第二级菜单项上时,我的样式有效(鼠标悬停时它将是黑色的):
为什么我的样式不能在一级 MenuItem 上工作?