如何解决我们可以在 IsPressed 上自定义 WPF MenuItem 样式吗
MainWindow.xaml:
<mah:MetroWindow x:Class="Wpf_MahAppsTEST.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:local="clr-namespace:Wpf_MahAppsTEST"
mc:Ignorable="d"
Title="MainWindow" Height="850" Width="800">
<mah:MetroWindow.Resources>
<SolidColorBrush x:Key="MahApps.Brushes.MenuItem.SelectionFill" Color="{StaticResource MahApps.Colors.Accent3}" />
<Style BasedOn="{StaticResource MahApps.Styles.MenuItem}" targettype="MenuItem" x:Key="MenuStyle">
<Setter Property="Background" Value="{StaticResource MahApps.Brushes.Accent}"/>
<Setter Property="Foreground" Value="{StaticResource MahApps.Brushes.IdealForeground}"/>
</Style>
</mah:MetroWindow.Resources>
<Grid>
<Menu>
<MenuItem Header="_File">
<MenuItem.Resources>
<Style targettype="{x:Type MenuItem}" BasedOn="{StaticResource MenuStyle}"/>
</MenuItem.Resources>
<MenuItem Header="_New" />
<MenuItem Header="_Open" />
<MenuItem Header="_Save" />
</MenuItem>
</Menu>
</Grid>
</mah:MetroWindow>
上面窗口的显示:
如下图,当我们按下(Ref: MenuITem.IsPressed)菜单的File
项时,File
项的背景显示为默认(灰色)。 问题当您将鼠标悬停在子项上时(例如File
项目如下图所示)?
例如,(没有 MahApps),默认外观如下 - File 项和 Open
项的背景匹配。我在上面的 MahApp 示例中寻找类似的场景:
解决方法
只需添加另一个画笔资源:
<mah:MetroWindow.Resources>
<SolidColorBrush x:Key="MahApps.Brushes.TopMenuItem.PressedFill"
Color="{StaticResource MahApps.Colors.Accent3}" />
<SolidColorBrush x:Key="MahApps.Brushes.MenuItem.SelectionFill" Color="{StaticResource MahApps.Colors.Accent3}" />
<Style BasedOn="{StaticResource MahApps.Styles.MenuItem}" TargetType="MenuItem" x:Key="MenuStyle">
<Setter Property="Background" Value="{StaticResource MahApps.Brushes.Accent}"/>
<Setter Property="Foreground" Value="{StaticResource MahApps.Brushes.IdealForeground}"/>
</Style>
</mah:MetroWindow.Resources>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。