我们可以在iPressed上自定义WPF菜单项样式吗

我们可以在iPressed上自定义WPF菜单项样式吗,wpf,menuitem,mahapps.metro,Wpf,Menuitem,Mahapps.metro,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" x

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>

以上窗口的显示:

<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>
如下所示,当我们按下菜单的
文件
项()时,
文件
项的背景显示为默认(灰色)问题当您将鼠标移到子项上时,如何使
文件
项的背景看起来与其子项的背景相同(如下面描述的
打开
项的背景)

例如,(不带MahApps),默认外观如下-其中文件项和
打开的
项的背景匹配。我在上面的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>

只需添加另一个笔刷资源:

<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>