C# 覆盖默认的 ContextMenu 样式 (WPF)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13147344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# override default ContextMenu Style (WPF)
提问by Estefano Salazar
I am having a problem trying to change the style of the default ContextMenu in WPF. I not want to override de ContextMenu, I simple want to override the ContextMenu STYLE. In all the sites, says that I must create each MenuItem of the ContextMenu, but I want to use the defaults MenuItems, and only change the style and add a border in the background. How can I do it?
我在尝试更改 WPF 中默认 ContextMenu 的样式时遇到问题。我不想覆盖 ContextMenu,我只想覆盖 ContextMenu STYLE。在所有站点中,都说我必须创建 ContextMenu 的每个 MenuItem,但我想使用默认值 MenuItems,并且只更改样式并在背景中添加边框。我该怎么做?
<Style TargetType="{x:Type ContextMenu}">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Grid.IsSharedSizeScope" Value="true" />
<Setter Property="HasDropShadow" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContextMenu}">
<Border x:Name="Border" Background="#000" BorderThickness="1">
<ScrollViewer x:Name="ScrollViewer">
<ItemsPresenter />
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
回答by NoOne
Make sure that :
确保:
- The resource dictionary is loaded and not unloaded later on in your code.
(As far as I remember the list of loaded resource dictionaries can be found in
Application.Current.Resources.MergedDictionaries) - You don't set the Style or other properties of the target ContextMenu in it's declaration.
- 资源字典在您的代码中稍后加载而不是卸载。(据我所知,加载的资源字典列表可以在 中找到
Application.Current.Resources.MergedDictionaries) - 您不在其声明中设置目标 ContextMenu 的 Style 或其他属性。

