2
votes

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?

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

1 Answers

2
votes

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.