WPF 上下文菜单设计。如何在WPF菜单项中设置背景?

发布于 2024-08-09 17:56:17 字数 932 浏览 2 评论 0原文

我创建这样的弹出菜单。

<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颜色或透明,发髻不是白色!!!!!! 如何修复它? :)

http://itrash.ru/idb/40e872e71346dcf9bd58ba8aec0b2a17/omenu.png.html< /a> - 菜单屏幕

截图XP下没问题。仅在 Vista 上菜单为白色(没有 W7)

I create popup menu like this.

<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>

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 - menu screenshot

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掀纱窥君容 2024-08-16 17:56:17

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

<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>

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>
丿*梦醉红颜 2024-08-16 17:56:17

如果您要为上下文菜单声明自定义样式,这样对于所有操作系统来说都是相同的。

If you will declare a custom style for your context menu, This way it will be same for all OS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文