无法为 IsPressed 事件设置 MenuItem 的背景属性 [英] Unable to set Background property of MenuItem for IsPressed event

查看:28
本文介绍了无法为 IsPressed 事件设置 MenuItem 的背景属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在按下 MenuItem 时更改 MenuItem 的背景.

I want to change the background of a MenuItem when the MenuItem is pressed.

<Style x:Key="{x:Type MenuItem}" TargetType="MenuItem">
    <Style.Triggers>
        <Trigger Property="MenuItem.IsPressed" Value="True">
            <Setter Property="MenuItem.Background" Value="#FFE389" />
            <Setter Property="MenuItem.BorderBrush" Value="#C2762B" />
        </Trigger>
    </Style.Triggers>
</Style>

我尝试执行上述操作,但触发器似乎不起作用.触发器错了吗?

I tried doing the above, but the trigger does not seem to work. Is the Trigger wrong?

更新:它适用于事件 IsMouseOver 但 IsPressed 似乎不起作用

Update: It works for the event IsMouseOver but IsPressed does not seem to work

更新 2:它适用于 TopLevelMenuItems,但不适用于 TopLevelMenuHeaderItems.

Update 2: It works for TopLevelMenuItems but does not work for TopLevelMenuHeaderItems.

推荐答案

试试这个...它不以 MenuItem 开头的属性名称和修改你的 TargetTypex:Key 语法...

Try this...which does not preface the property names with MenuItem and modify your TargetType and x:Key syntax...

<Style x:Key="MyStyle" TargetType="{x:Type MenuItem}">
    <Style.Triggers>
        <Trigger Property="IsPressed" Value="True">
            <Setter Property="Background" Value="#FFE389" />
            <Setter Property="BorderBrush" Value="#C2762B" />
        </Trigger>
    </Style.Triggers>
</Style>

根据您的更新,查看默认 MenuItem通过 XAML 构建.这应该可以帮助您为 MenuItem 的不同部分提供样式.请注意在 MenuItem 样式中使用了 Role 属性来处理顶级和子级别的标题和项目.

Based on your updates take a look at how a default MenuItem is constructed via XAML. This should get you where you need to go in providing styling for the varying parts of the MenuItem. Note the use of the Role property within the MenuItem style dealing with the headers and items at both the top level and sub level.

这篇关于无法为 IsPressed 事件设置 MenuItem 的背景属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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