WPF ContextMenu设计.如何在WPF MenuItem中设置背景? [英] WPF ContextMenu Design. How to set Background in WPF MenuItem?

查看:1148
本文介绍了WPF ContextMenu设计.如何在WPF MenuItem中设置背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样创建弹出菜单.

<DockPanel.ContextMenu>
    <ContextMenu Background="#CD252220" Opacity="0.95" Foreground="LightGray" BorderBrush="DarkGray">
        <MenuItem Header="_Save Image..." x:Name="btSave" IsEnabled="False" Click="btSave_Click" Style="{StaticResource MyStyle}">
            <MenuItem.Icon>
                <Image Source="icons/save.png" Width="16" Height="16" Style="{StaticResource IconStyle}"/>
            </MenuItem.Icon>
        </MenuItem>
    </ContextMenu>
</DockPanel.ContextMenu>

为什么此菜单的左侧为白色????? 它将是#CD252220颜色或透明的,发bun而不是白色的! 如何解决? :)

Why left-side of this menu is WHITE????? It'll be a #CD252220 color or transparent, bun not white!!!!!! How to fix it? :)

http://itrash.ru/idb/40e872e71346dcf9bd58ba8aec0b2a17/omenu.png.html -菜单截图

P.S.在XP中可以.菜单仅在Vista(没有W7)上为白色

P.S. In XP it's OK. Menu is White only on Vista (don't have W7)

推荐答案

我找到了解决方案!您只需要在样式定义"部分中设置属性 OverridesDefaultStyle 即可;)

I find solution! You have to just set property OverridesDefaultStyle in Style-defenition section ;)

<Style x:Key="{x:Type ContextMenu}" TargetType="{x:Type ContextMenu}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContextMenu}">
<Border Background="#CD222120" CornerRadius="7, 7, 8, 8" BorderBrush="DarkGray" BorderThickness="2" Opacity="0.96">
<StackPanel ClipToBounds="True" Orientation="Vertical" IsItemsHost="True" Margin="5,4,5,4"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="{x:Static MenuItem.TopLevelItemTemplateKey}" TargetType="{x:Type MenuItem}">
<Border Name="Border" >
<Grid>
<ContentPresenter Margin="6,3,6,3" ContentSource="Header" RecognizesAccessKey="True" />
</Grid>
</Border>
</ControlTemplate>

这篇关于WPF ContextMenu设计.如何在WPF MenuItem中设置背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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