简体   繁体   English

如何删除 ContextMenu 边框

[英]How to remove ContextMenu border

So im trying to make button appear on right click in my ListBox .所以我试图让按钮出现在我的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.这是我的MenuItem模板。

<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.您识别为白色边框的是包含您的MenuItemContextMenu本身。 Try adding more buttons and changing the Background and BorderBrush of the ContextMenu and you will see.尝试添加更多按钮并更改ContextMenuBackgroundBorderBrush ,您会看到。

<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.如果您为MenuItems创建自定义控件模板,您可能也应该为ContextMenu这样做,如果仅设置画笔不符合您的要求。 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.如果需要,请查看此相关帖子以获取有关如何为ContextMenu提取默认控件模板的指导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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