1

I have a background with a custom color for a ContextMenu. I added in a separator as so (between different menu items):

<Separator Background="#EDECEC" Margin="0" ></Separator>

The background color is #edecec. However, I see a separator, and the color doesn't match the rest of the contextmenu. It is lighter than the contextmenu. Is there a way to change that? Thanks.

H.B.
  • 166,899
  • 29
  • 327
  • 400
Crystal
  • 28,460
  • 62
  • 219
  • 393

1 Answers1

4

The Separator in menus has a default Template which ignores the Background, to override it add a respective style to some ancestor's Resources using the right key:

<Style x:Key="{x:Static MenuItem.SeparatorStyleKey}"
       TargetType="{x:Type Separator}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Separator}">
                <!-- ControlTemplate with a TemplateBinding to Background here -->
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Could I achieve the same effect by just drawing a line instead? – Crystal Aug 31 '11 at 20:55
  • 1
    Visually probably yes, but you'd need to make sure the containing MenuItem is disabled, you normally don't want a clickable separator. That being the case actually using a separator might be best as it is meant for this and integrated into the framework. – H.B. Aug 31 '11 at 20:59
  • Can you provide an example of what that TemplateBinding would look like? Dumb question, I know, but I'm running my head into a wall (Unfamiliar with XAML) – LeesusFreak Jan 29 '16 at 19:38
  • @LeesusFreak: Should be something like `Background="{TemplateBinding Background}"`, or if the template uses a shape it'll probably be a `Fill` that is bound to `Background`. – H.B. Jan 30 '16 at 15:01