There are two MenuItem'a separator between them, but how to display it under the icon? enter image description here

MenuItem xaml

<Button Content="Right-click me!" VerticalAlignment="Center" HorizontalAlignment="Center"> <Button.ContextMenu> <ContextMenu> <MenuItem Header="Menu item 1" /> <MenuItem Header="Menu item 2" /> <Separator /> <MenuItem Header="Menu item 3" /> </ContextMenu> </Button.ContextMenu> </Button> 
  • one
    And how do you display the icon? Show your XAML. - Bulson
  • one
    @Bulson added a question - Adam Hodovanets
  • @AdamHodovanets, is it possible to have a minimal self-sufficient example, such as to insert into a studio and launch, but at the same time, nothing superfluous? - Andrey NOP
  • one
    @AndreyNOP Yes, in general, that simple context menu, there separator is drawn already after the icons - Adam Hodovanets February

1 answer 1

The standard separator pattern is:

 <ControlTemplate TargetType="{x:Type Separator}"> <Grid Margin="0,6,0,4" SnapsToDevicePixels="true" UseLayoutRounding="False"> <Rectangle Fill="#E0E0E0" Height="1" Margin="30,0,1,1"/> <Rectangle Fill="White" Height="1" Margin="30,1,1,0"/> </Grid> </ControlTemplate> 

As you can see, it is indented using Margin , just fix this:

 <Window.Resources> <ControlTemplate x:Key="MySeparatorTemplate" TargetType="{x:Type Separator}"> <Grid Margin="0,6,0,4" SnapsToDevicePixels="true" UseLayoutRounding="False"> <Rectangle Fill="#E0E0E0" Height="1" Margin="1,0,1,1"/> <Rectangle Fill="White" Height="1" Margin="1,1,1,0"/> </Grid> </ControlTemplate> </Window.Resources> <Button Content="Right-click me!" VerticalAlignment="Center" HorizontalAlignment="Center"> <Button.ContextMenu> <ContextMenu> <MenuItem Header="Menu item 1" /> <MenuItem Header="Menu item 2" /> <Separator Template="{StaticResource MySeparatorTemplate}"/> <MenuItem Header="Menu item 3" /> </ContextMenu> </Button.ContextMenu> </Button> 

enter image description here