简体   繁体   English

如何在WPF的自定义MenuItem控件中将鼠标悬停在触发器上?

[英]How to set the mouse over trigger in custom MenuItem Control of WPF?

I have a MenuItem style: 我有一个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>

My Control is: 我的控制权是:

<Grid>
<Menu VerticalAlignment="Center" HorizontalAlignment="Center">
    <MenuItem Header="Header">
        <MenuItem Header="Second Level"/>
    </MenuItem>
</Menu>

I am willing to set a new style when mouse over the menuitem, the background will be black. 当菜单上的鼠标悬停时,我愿意设置一种新样式,背景为黑色。 My style works but only the second level menuitem. 我的样式有效,但仅适用于第二级菜单项。

When mouse over the first level menuitem, it still the default style: 将鼠标悬停在第一级菜单项上时,它仍是默认样式: 一级菜单项

When mouse over the second level menuitem, my style works(it will be black when mouse over it): 当将鼠标悬停在第二级菜单项上时,我的样式有效(将鼠标悬停在其上时它将为黑色): 在此处输入图片说明

Why my style can not work at first level MenuItem? 为什么我的样式不能在一级菜单项上使用?

Control Template for Top Level MenuItem and Sub-MenuItems are different. 顶级MenuItem和Sub-MenuItem的控制模板是不同的。 Top level MenuItem also needs to define how the SubMenu Items will Popup. 顶级MenuItem还需要定义子菜单项将如何弹出。

You can write multiple ControlTemplate for each type of Menu Item. 您可以为每种菜单项类型编写多个ControlTemplate。 And then use following Style to Set the MenuItem styles: 然后使用以下样式设置MenuItem样式:

  <Style x:Key="{x:Type MenuItem}" TargetType="MenuItem"> <Setter Property="OverridesDefaultStyle" Value="True"/> <Style.Triggers> <Trigger Property="Role" Value="TopLevelHeader"> <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.TopLevelHeaderTemplateKey}}"/> <Setter Property="Grid.IsSharedSizeScope" Value="true"/> </Trigger> <Trigger Property="Role" Value="TopLevelItem"> <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.TopLevelItemTemplateKey}}"/> </Trigger> <Trigger Property="Role" Value="SubmenuHeader"> <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.SubmenuHeaderTemplateKey}}"/> </Trigger> <Trigger Property="Role" Value="SubmenuItem"> <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.SubmenuItemTemplateKey}}"/> </Trigger> </Style.Triggers> </Style> 

For more detail see this post 有关更多详细信息,请参阅此帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM