如何在 MouseOver 上 remove/change 突出显示 MenuItem/ContextMenu 的颜色?
How do I remove/change hilghlighted color of MenuItem/ContextMenu on MouseOver?
如何在 MouseOver 上 remove/change 突出显示 MenuItem/ContextMenu 的颜色?我尝试编辑 ContextMenu 模板并在 MouseOver 上为 MenuItem 设置样式,但默认的 HiglightedColor 在 MouseOver 上仍然存在。
<Style TargetType="{x:Type ContextMenu}">
<Setter Property="SnapsToDevicePixels"
Value="True" />
<Setter Property="OverridesDefaultStyle"
Value="True" />
<Setter Property="Grid.IsSharedSizeScope"
Value="true" />
<Setter Property="HasDropShadow"
Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContextMenu}">
<Border x:Name="Border"
Background="Black"
BorderThickness="0">
<Border.BorderBrush>
<SolidColorBrush Color="Black" />
</Border.BorderBrush>
<StackPanel IsItemsHost="True"
KeyboardNavigation.DirectionalNavigation="Cycle" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="HasDropShadow"
Value="true">
<Setter TargetName="Border"
Property="Padding"
Value="0,0,0,0" />
<Setter TargetName="Border"
Property="CornerRadius"
Value="0" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Background" Value="Transparent"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type MenuItem}">
<Style.Triggers>
<Trigger Property="IsHighlighted" Value="True">
<Setter Property="Background" Value="Transparent"></Setter>
</Trigger>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="Transparent">
</Setter>
</Trigger>
</Style.Triggers>
</Style>
您必须覆盖 MenuItem
默认模板并在 IsHighlighted
上设置触发器 属性:
<ContextMenu>
<ContextMenu.Resources>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter Content="{TemplateBinding Header}" Margin="35 5 10 5" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsHighlighted" Value="True">
<Setter Property="Background" Value="Green"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ContextMenu.Resources>
<MenuItem Header="Element 1" />
<MenuItem Header="Element 2" />
<MenuItem Header="Element 3" />
</ContextMenu>
要在项目悬停时删除背景,只需将背景颜色设置为 Transparent
。
如何在 MouseOver 上 remove/change 突出显示 MenuItem/ContextMenu 的颜色?我尝试编辑 ContextMenu 模板并在 MouseOver 上为 MenuItem 设置样式,但默认的 HiglightedColor 在 MouseOver 上仍然存在。
<Style TargetType="{x:Type ContextMenu}">
<Setter Property="SnapsToDevicePixels"
Value="True" />
<Setter Property="OverridesDefaultStyle"
Value="True" />
<Setter Property="Grid.IsSharedSizeScope"
Value="true" />
<Setter Property="HasDropShadow"
Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContextMenu}">
<Border x:Name="Border"
Background="Black"
BorderThickness="0">
<Border.BorderBrush>
<SolidColorBrush Color="Black" />
</Border.BorderBrush>
<StackPanel IsItemsHost="True"
KeyboardNavigation.DirectionalNavigation="Cycle" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="HasDropShadow"
Value="true">
<Setter TargetName="Border"
Property="Padding"
Value="0,0,0,0" />
<Setter TargetName="Border"
Property="CornerRadius"
Value="0" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Background" Value="Transparent"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type MenuItem}">
<Style.Triggers>
<Trigger Property="IsHighlighted" Value="True">
<Setter Property="Background" Value="Transparent"></Setter>
</Trigger>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="Transparent">
</Setter>
</Trigger>
</Style.Triggers>
</Style>
您必须覆盖 MenuItem
默认模板并在 IsHighlighted
上设置触发器 属性:
<ContextMenu>
<ContextMenu.Resources>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter Content="{TemplateBinding Header}" Margin="35 5 10 5" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsHighlighted" Value="True">
<Setter Property="Background" Value="Green"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ContextMenu.Resources>
<MenuItem Header="Element 1" />
<MenuItem Header="Element 2" />
<MenuItem Header="Element 3" />
</ContextMenu>
要在项目悬停时删除背景,只需将背景颜色设置为 Transparent
。