在WPF中,在MenuItem的模板中,如果在IsHighlighted触发器中设置Background,则Background IsPressed触发器无效

时间:2013-09-04 05:17:50

标签: wpf templates triggers styles menuitem

我希望MenuItem具有不同背景的IsMouseOver和IsPressed状态,但经过一些尝试后,仍然会失败。

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

SubmarineX ..菜单包含许多子控件..如果你想设置Menuitems的背景颜色..那么你需要为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定义样式....希望这有助于......