当鼠标进入WPF时,我们如何在WPF的菜单背景中添加颜色? [英] How we can add color in menu background in WPF when mouse enter on it?
本文介绍了当鼠标进入WPF时,我们如何在WPF的菜单背景中添加颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我们触发像IsMouseOver这样的触发器时,如何设置菜单项的背景颜色。
我尝试了什么:
我将在wpf中使用模板和触发器,但这将无法正常工作
How to set background color of an menuitem when we fire trigger like IsMouseOver.
What I have tried:
I will use an templates and triggers in wpf, but this will not working properly
推荐答案
<pre> <LinearGradientBrush x:Key="DarkBrush" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="#939393" Offset="0.0"/>
<GradientStop Color="#717171" Offset="0.05"/>
<GradientStop Color="#606060" Offset="1.0"/>
</GradientStopCollection>
</GradientBrush.GradientStops>
</LinearGradientBrush>
<LinearGradientBrush x:Key="Clicked" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="#363636" Offset="0.0"/>
<GradientStop Color="#393939" Offset="1.0"/>
</GradientStopCollection>
</GradientBrush.GradientStops>
</LinearGradientBrush>
<Style x:Key="{x:Type Menu}" TargetType="Menu">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Menu">
<Border x:Name="MainMenu" Background="Black">
<StackPanel ClipToBounds="True" Orientation="Horizontal" IsItemsHost="True"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="{x:Static MenuItem.SubmenuItemTemplateKey}" TargetType="MenuItem">
<Border Background="Black" x:Name="_grid" Padding="0" BorderThickness="0" BorderBrush="Black" >
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="24" SharedSizeGroup="IconGroup"/>
<ColumnDefinition Width="Auto" MinWidth="30" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20"></RowDefinition>
</Grid.RowDefinitions>
<ContentPresenter ContentSource="Icon" Margin="{TemplateBinding Padding}" />
<ContentPresenter Grid.Column="1" Grid.Row="0" VerticalAlignment="Center" ContentSource="Header" RecognizesAccessKey="True" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsHighlighted" Value="True">
<Setter Property="Background" Value="DarkGray" TargetName="_grid"/>
<Setter Property="BorderBrush" Value="DarkGray" TargetName="_grid"/>
<Setter Property="Foreground" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
这篇关于当鼠标进入WPF时,我们如何在WPF的菜单背景中添加颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文