我正在尝试创建一个上下文菜单,允许用户查看其下的内容,因此我为ContextMenu及其MenuItems Transparent创建了Background。当用户将鼠标移到MenuItems(悬停)上时,由于IsHighlighted上的ControlTemplate触发器,它们应该显示出来。问题是,当鼠标位于MenuItems的透明部分上时,Trigger无法正常工作。如果我将ContextMenu或其MenuItems的背景颜色更改为透明以外的其他内容,则触发器可以正常工作。我认为透明背景应该表现得像纯色,但在这种情况下它不是。任何想法如何使这项工作?
<Window x:Class="TransparentContextMenu.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="600" Width="800">
<Window.Resources>
<ControlTemplate x:Key="contextMenuItemTemplate" TargetType="{x:Type MenuItem}">
<Grid>
<Polygon Points="0,0 180,0 180,30 0,30" Fill="Transparent" x:Name="Background"/>
<Polygon Points="0,0 180,0 180,30 0,30" Opacity="0" x:Name="HoverBorder" Fill="Gray"/>
<ContentPresenter ContentSource="Header" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsHighlighted" Value="true">
<Setter Property="Opacity" Value="1" TargetName="HoverBorder"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="contextMenuStyle" TargetType="{x:Type ContextMenu}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContextMenu}">
<Border Background="Transparent">
<StackPanel IsItemsHost="True"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid Background="Transparent">
<Grid.ContextMenu>
<ContextMenu Style="{StaticResource contextMenuStyle}">
<MenuItem Header="Item A" Template="{StaticResource contextMenuItemTemplate}"/>
<MenuItem Header="Item B" Template="{StaticResource contextMenuItemTemplate}"/>
<MenuItem Header="Item C" Template="{StaticResource contextMenuItemTemplate}"/>
<MenuItem Header="Item D" Template="{StaticResource contextMenuItemTemplate}"/>
<MenuItem Header="Item E" Template="{StaticResource contextMenuItemTemplate}"/>
</ContextMenu>
</Grid.ContextMenu>
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
答案 0 :(得分:0)
尝试将背景设置为'null'。