开发者

Unable to set Background property of MenuItem for IsPressed event

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" 开发者_如何学Python/>
            <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?

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

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

Unable to set Background property of MenuItem for IsPressed event


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>

EDIT:

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜