Reputation: 1271
I am relatively new to WPF I am trying to change the Hover effect of the menu item and set it similar to the one of my buttons in the Toolbar.
I am left with a double border when I hover on the menu most probably from the MenuItem. How can I remove it?
XAML File:
<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>
Resource File:
<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>
Upvotes: 0
Views: 8037
Reputation: 95
Lets say you have your menu items in a Listbox
, and when you move over your mouse you want colors to change. Here is a simple way to do it, but I have copied the code from my existing project:
<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>
hope this helps
Upvotes: 0
Reputation: 19294
Your 'double border' issue cannot be solve at the Menu level : you have to dig further and redefine a template / Trigger for the MenuItem of your menu. MenuItem (not Menu) has default behaviour of turning its Border color to grey on MouseOver, and that is what you want to change.
Edit : so, for instance, you might define a default style for MenuItem with a setter on BorderWidth = 0.
Upvotes: 1