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

问题描述 投票:0回答:2
wpf xaml transparency contextmenu menuitem
2个回答
8
投票

我找到解决方案了!您只需在样式定义部分设置属性 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>

-3
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.