C# 覆盖默认的 ContextMenu 样式 (WPF)

标签 c# wpf contextmenu

我在尝试更改 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>

最佳答案

确保:

  • 资源字典会在代码中加载,但稍后不会卸载。 (据我记得加载的资源字典列表可以在 Application.Current.Resources.MergedDictionaries 中找到)
  • 您无需在 objective-c ontextMenu 的声明中设置其 Style 或其他属性。

关于C# 覆盖默认的 ContextMenu 样式 (WPF),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13147344/

相关文章:

c# - 在启动线程之前无法更改 WPF 控件状态

WPF:您如何在横向模式下打印?

javascript - 取决于所选文本的动态扩展上下文菜单

java - 我应该使用什么命令来使用 Windows 注册表从上下文菜单启动 jar 文件?

C# HTML 字体标签解析

c# - 在 winforms 中,我可以将其中包含一堆控件(如列表框)的控件拖到另一个控件中并从那里调整大小/移动它们吗?

c# - 按下按钮时绑定(bind)(显式绑定(bind))始终将 BindingExpression 设置为 null?

body 溢出的JavaScript?

javascript - 如何调用MVC telerik网格刷新按钮的onclick事件

c# - System.AccessViolationException 的奇怪问题