WPF ContextMenu Design. How to set Background in WPF MenuItem?

▼魔方 西西 提交于 2020-02-02 13:21:07

问题


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)


回答1:


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>



回答2:


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



来源:https://stackoverflow.com/questions/1673132/wpf-contextmenu-design-how-to-set-background-in-wpf-menuitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!