我对WPF比较陌生,我正在尝试更改菜单项的悬停效果,并将其设置为类似于工具栏中的一个按钮。
当我将鼠标悬停在菜单上时,我会留下一个双边框,很可能是从MenuItem。如何移除它?
XAML文件:
<ToolBarTray Style="{StaticResource MainToolBar}">
<ToolBar DockPanel.Dock="Top" ToolBarTray.IsLocked="True" Width="Auto" Padding="0" Background="Transparent" Name="Tool">
<Button Content="Button 1"/>
<Button Content="Button 2"/>
<Separator/>
<Menu Margin="0, -1, 0, 0" Style="{StaticResource Menu}">
<MenuItem Header="Menu">
<MenuItem Header="File">
<MenuItem Header="Copy"/>
<MenuItem Header="Paste"/>
</MenuItem>
</MenuItem>
</Menu>
</ToolBar>
</ToolBarTray>
资源文件:
<Style x:Key="{x:Static ToolBar.ButtonStyleKey}" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border
SnapsToDevicePixels="true"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}">
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" Value="Orange"/>
<Setter Property="Background" Value="{DynamicResource ToolBarBKHover}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="Menu" TargetType="{x:Type Menu}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Menu}">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel ClipToBounds="True" Orientation="Horizontal" IsItemsHost="True"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="Orange"/>
<Setter Property="Background" Value="{DynamicResource ToolBarBKHover}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
您的"双边界"问题无法在菜单级别解决:您必须进一步挖掘并为菜单的MenuItem重新定义模板/触发器。MenuItem(而不是Menu)的默认行为是在鼠标悬停时将其边框颜色变为灰色,这就是您想要更改的。
编辑:例如,您可以为MenuItem定义一个默认样式,并在BorderWidth=0上设置一个setter。
假设您的菜单项位于Listbox
中,当您移动鼠标时,您希望更改颜色。这里有一个简单的方法,但我已经从我现有的项目中复制了代码:
<Window.Resources>
<Style TargetType="{x:Type ListBox}">
<Setter Property="Foreground" Value="#58290a" />
<Setter Property="FontFamily" Value="Lucida Console" />
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
<LinearGradientBrush.GradientStops>
<GradientStop Color="#feca00" Offset="0.1"/>
<GradientStop Color="#ffe100" Offset="0.4"/>
<GradientStop Color="#feca00" Offset="0.6"/>
<GradientStop Color="Orange" Offset="0.9"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BitmapEffect">
<Setter.Value>
<OuterGlowBitmapEffect GlowColor="Red" GlowSize="4"/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
希望这对有帮助