在鼠标悬停时更改 MenuItem 的颜色 [英] Change color of MenuItem on MouseOver

查看:29
本文介绍了在鼠标悬停时更改 MenuItem 的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 mouseOver 处更改 MenuItem 的颜色.我还需要圆角边框、图像和文本框.当我设置样式时一切正常,只有 mouseOverEvent 正在做任何事情,背景不会改变.我的代码是:

I want to change the color of a MenuItem at mouseOver. I need also rounded borders, an image and a textBox. When I set the style all is ok only the mouseOverEvent is doing anything, the background doesnot change. My code is:

  <Style x:Key="BaseStyle" TargetType="MenuItem">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Background" Value="#0a99f3" />
            </Trigger>
            <Trigger Property="IsKeyboardFocusWithin" Value="true">
                <Setter Property="Background" Value="#0a99f3" />
            </Trigger>
        </Style.Triggers>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type MenuItem}">
                    <Grid>
                        <Border Name="MainBorder" BorderThickness="2,2,2,0" CornerRadius="8,8,8,8" Margin="0,0,1,0"  BorderBrush="AliceBlue">

                            <Grid>
                                <TextBlock Text="Info" Margin="30,10,0,0" FontFamily="Arial" FontSize="14" FontWeight="Bold" />
                                <Image Width="15" Height="15" Source="menu.PNG" Margin="-100,0,0,0" />
                            </Grid>
                        </Border>

                    </Grid>
                </ControlTemplate>

            </Setter.Value>
        </Setter>

    </Style>    

希望有人知道我错过了什么.谢谢!

Hope anybody know what I am missing. Thanks!

推荐答案

您正在覆盖模板,但没有在其中的任何地方使用背景颜色,因此永远不会应用该值.

You're overwritting the Template, but not using the Background Color anywhere in it so the value never gets applied.

在菜单项模板中设置背景颜色

Set the Background Color in your MenuItem Template

<ControlTemplate TargetType="{x:Type MenuItem}">
    <Grid Background="{TemplateBinding Background}">

这篇关于在鼠标悬停时更改 MenuItem 的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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