我们可以在 IsPressed 上自定义 WPF MenuItem 样式吗
Can we customize WPF MenuItem style on IsPressed
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>
以上显示window:
如下图,当我们按下(Ref: MenuITem.IsPressed)菜单的File
项时,File
项的背景显示为默认(灰色)。 问题当鼠标悬停在子项上时,如何使File
项的背景看起来与其子项的背景相同(例如[=的背景14=] 项目如下图所示)?
例如,(没有 MahApps),默认外观如下 - 文件项和 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>
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>
以上显示window:
如下图,当我们按下(Ref: MenuITem.IsPressed)菜单的File
项时,File
项的背景显示为默认(灰色)。 问题当鼠标悬停在子项上时,如何使File
项的背景看起来与其子项的背景相同(例如[=的背景14=] 项目如下图所示)?
例如,(没有 MahApps),默认外观如下 - 文件项和 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>