0

我希望 MenuItem 具有不同背景的 IsMouseOver 和 IsPressed 状态,但尝试几次后仍然失败。

4

2 回答 2

1

尝试使用这种风格

<Window.Resources>
    <Style TargetType="{x:Type MenuItem}">
        <Setter Property="Background" Value="Red"></Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Green"></Setter>
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Background" Value="Blue"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
于 2013-09-04T05:48:17.063 回答
1

SubmarineX ..菜单由很多子控件组成..如果你想设置菜单项的背景颜色..那么你需要为Out菜单项和内部菜单项定义样式......

 <Grid>
    <Menu IsMainMenu="True">
        <Menu.Resources>
            <!-- Outermenu items-->
            <Style TargetType="{x:Type MenuItem}">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Red" />
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Background" Value="Green" />
                    </Trigger>
                </Style.Triggers>
            </Style>

            <!-- Submenu items -->
            <ControlTemplate x:Key="{x:Static MenuItem.SubmenuItemTemplateKey}"
                 TargetType="{x:Type MenuItem}">
                <Border Name="Border">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" SharedSizeGroup="Icon" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut" />
                            <ColumnDefinition Width="13" />
                        </Grid.ColumnDefinitions>
                        <ContentPresenter Name="Icon" Margin="6,0,6,0" VerticalAlignment="Center" ContentSource="Icon" />
                        <Border Name="Check" Width="13" Height="13" Visibility="Collapsed" Margin="6,0,6,0" Background="#0f3c5a"
                                BorderThickness="1" BorderBrush="#5082a4">
                            <Path Name="CheckMark" Width="7" Height="7" Visibility="Hidden" SnapsToDevicePixels="False"
                                  Stroke="#5082a4" StrokeThickness="2" Data="M 0 0 L 7 7 M 0 7 L 7 0" />
                        </Border>
                        <ContentPresenter Name="HeaderHost" Grid.Column="1" ContentSource="Header" RecognizesAccessKey="True" />
                        <TextBlock x:Name="InputGestureText" Grid.Column="2" Text="{TemplateBinding InputGestureText}" 
                                   Margin="5,2,0,2" DockPanel.Dock="Right" />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsHighlighted" Value="true">
                        <Setter TargetName="Border" Property="Background" Value="Yellow" />
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="Blue" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Menu.Resources>

        <MenuItem Header="_File">
            <MenuItem Header="_New"/>
            <MenuItem Header="_Open"/>
        </MenuItem>
        <MenuItem Header="_Edit">
            <MenuItem Header="_Cut">
                <MenuItem.Icon>
                    <Image Source="Images/cut.ico" Height="16" Width="16" />
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="_Copy">
                <MenuItem.Icon>
                    <Image Source="Images/copy.ico" Height="16" Width="16" />
                </MenuItem.Icon>
                <MenuItem Header="_Y"/>
                <MenuItem Header="_X"/>
            </MenuItem>
            <MenuItem Header="_Paste">
                <MenuItem.Icon>
                    <Image Source="Images/paste.ico" Height="16" Width="16" />
                </MenuItem.Icon>
            </MenuItem>
        </MenuItem>
        <MenuItem Header="_View" />
        <MenuItem Header="_Window" />
        <MenuItem Header="_Help" />
    </Menu>
</Grid>

如果您还想设置菜单标题的样式,那么您还需要为 MenuItem.SubmenuItemTemplateKey 定义样式......希望这会有所帮助......

于 2013-09-04T06:10:15.717 回答