[英]How to remove ContextMenu border
所以我試圖讓按鈕出現在我的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>
這是我的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>
一切都很好,但按鈕周圍有白色邊框。
您識別為白色邊框的是包含您的MenuItem
的ContextMenu
本身。 嘗試添加更多按鈕並更改ContextMenu
的Background
和BorderBrush
,您會看到。
<ContextMenu Background="Red" BorderBrush="Blue">
像這樣改變畫筆會導致這個結果,這很明顯。
如果您為MenuItems
創建自定義控件模板,您可能也應該為ContextMenu
這樣做,如果僅設置畫筆不符合您的要求。 正如您在示例中看到的那樣,仍然有一條垂直白線是默認控件模板的一部分,您可能希望去掉它。 您可以從這個示例開始,盡管它既不是默認模板也不是完整的。 如果需要,請查看此相關帖子以獲取有關如何為ContextMenu
提取默認控件模板的指導。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.