MenuItem 的背景颜色在 MouseOver 上没有改变 [英] Background color for MenuItem not changed on MouseOver

查看:28
本文介绍了MenuItem 的背景颜色在 MouseOver 上没有改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三个项目的菜单,当鼠标悬停在任何项目上时,我试图更改背景颜色.我试过 IsMouseOver &IsHighlighted 触发器属性,两者都不起作用.

I have a Menu with three items and I'm trying to change the background color when the mouse hovers over any of the items. I've tried the IsMouseOver & IsHighlighted trigger property and neither works.

在我的 App.xaml 中:

In my App.xaml:

<Style TargetType="MenuItem" x:Key="MenuItemStyle" >
        <Style.Triggers>
            <Trigger Property="MenuItem.IsHighlighted" Value="true">
                <Setter Property="Background" Value="Black"/>
            </Trigger>
        </Style.Triggers>          
    </Style>

在我的 Main.xaml 中:

In my Main.xaml:

<Menu HorizontalAlignment="Left" Height="30" VerticalAlignment="Top" Width="975
          " FontFamily="Tempus Sans ITC" FontSize="16"  >
        <Menu.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFC8C8C8" Offset="0"/>
                <GradientStop Color="Black" Offset="1"/>
            </LinearGradientBrush>
        </Menu.Background>
        <Menu.Foreground>
            <SolidColorBrush Color="#FFFFFFFB"/>
        </Menu.Foreground>
        <MenuItem Header="New" Click="MenuNew_Click" VerticalAlignment="Center" Padding="15,4,8,3" Width="60">
            <MenuItem.ToolTip>
                <ToolTip>
                    Add new Park
                </ToolTip>
            </MenuItem.ToolTip>
        </MenuItem>
        <MenuItem Header="Search" Width="65" Padding="12,4,8,3"  >
            <MenuItem.ToolTip>
                <ToolTip> Select search option</ToolTip>
            </MenuItem.ToolTip>
            <MenuItem Header="Name" Background="Black" FontSize="14" Style="{StaticResource MenuItemStyle}" />
            <MenuItem Header="ID" Background="Black" FontSize="14"/>
            <MenuItem Header="OwnerName" Background="Black" FontSize="14"/>
        </MenuItem>          
    </Menu>    

推荐答案

MenuItem 的默认 ControlTemplate(由 显示模板) 不会在鼠标悬停时设置 MenuItem.Background 属性,而是设置一个元素直接在模板中.不幸的是,这意味着您不能仅更改突出显示颜色,而是必须重新创建整个 ControlTemplate.MSDN 有一个示例说明如何这样做(这个来自 .NET 3.5,但应该适用于 4.0 或 4.5).

The default ControlTemplate for MenuItem (as extracted by Show Me The Template) doesn't set the MenuItem.Background property on mouse over, it sets an element in the template directly. Unfortunately, this means that you won't be able to just change the highlight color, but you'll have to recreate the entire ControlTemplate. MSDN has an example of how to do this (this one is from .NET 3.5 but should work for 4.0 or 4.5).

代码中的另一个警告:由于您直接在 MenuItem 上设置 Background,因此 Style>Trigger 无论如何都行不通.由于 DependencyProperty 值优先,本地您在项目上设置的值不能被 Style Trigger 覆盖.

One other caveat in your code: since you're setting the Background directly on the MenuItems, your Style's Trigger would not work anyway. Because of DependencyProperty value precedence, the local value you set on the items can not be overridden by a Style Trigger.

这篇关于MenuItem 的背景颜色在 MouseOver 上没有改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆