简体   繁体   中英

How to remove ContextMenu border

So im trying to make button appear on right click in my ListBox .

<ListBox Grid.Column="1" Margin="358,44,20,63" Name="scriptbox" Background="#FF282828" Foreground="White" SelectionChanged="Scriptbox_SelectionChanged" BorderThickness="0">
   <ListBox.ContextMenu>
      <ContextMenu>
         <MenuItem
            Template="{DynamicResource MenuItemTemplate}"
            Header="Delete" 
            Click="MenuItemDelete_Click" >
         </MenuItem>
      </ContextMenu>
   </ListBox.ContextMenu>

This is my MenuItem template.

<ControlTemplate TargetType="{x:Type MenuItem}" x:Key="MenuItemTemplate">
   <Border x:Name="Border" Background="#FF282828"  Padding="30,5,30,5" BorderThickness="0" Margin="0">
      <ContentPresenter ContentSource="Header" x:Name="HeaderHost" RecognizesAccessKey="True" />
   </Border>
   <ControlTemplate.Triggers>
      <Trigger Property="IsHighlighted" Value="true">
         <Setter Property="Background" TargetName="Border" Value="#51544e"/>
      </Trigger>
   </ControlTemplate.Triggers>
</ControlTemplate>
<ItemsPanelTemplate x:Key="MenuItemPanelTemplate">
   <StackPanel Margin="-3,0,0,0" Background="White"/>
</ItemsPanelTemplate>
<Style TargetType="{x:Type MenuItem}">
   <Setter Property="ItemsPanel" Value="{StaticResource MenuItemPanelTemplate}"/>
</Style>

Everything is fine but there is white border all around the button.

What you recognize as a white border is the ContextMenu itself that contains your MenuItem s. Try adding more buttons and changing the Background and BorderBrush of the ContextMenu and you will see.

<ContextMenu Background="Red" BorderBrush="Blue">

Changing the brushes like this will lead to this result, which makes it obvious.

在此处输入图像描述

If you create a custom control template for your MenuItems you should probably do so for the ContextMenu , too, if only setting brushes does not fit your requirements. As you can see in the example, there is still a vertical white line that is part of the default control template, that you might want to get rid of. You can start from this example , although it is neither the default template nor complete. Look at this related post for guidance on how to extract the default control template for ContextMenu if you need it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM