wpf - 仅在文本上方突出显示菜单项

时间:2011-07-28 15:23:15

标签: wpf styles

有人可以告诉我为什么只有当我将鼠标悬停在菜单文本上而不是悬停在菜单项的任何位置时,我的菜单项才会“突出显示”?这是我的菜单项样式:

            <Style x:Key="CtxMenuItemStyle" TargetType="{x:Type MenuItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}">
                        <Border x:Name="Border">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition x:Name="Col0" MinWidth="31" Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition x:Name="Col3" Width="20"/>
                                </Grid.ColumnDefinitions>

                                <!-- ContentPresenter to show an Icon if needed -->
                                <ContentPresenter Grid.Column="0" Margin="4,0,6,0" x:Name="Icon" VerticalAlignment="Center" ContentSource="Icon"/>

                                <!-- Glyph is a checkmark if needed for a checkable menu -->
                                <Grid Grid.Column="0" Visibility="Hidden" Margin="4,0,6,0" x:Name="GlyphPanel" VerticalAlignment="Center">
                                    <Path x:Name="GlyphPanelpath" VerticalAlignment="Center" Fill="{TemplateBinding Foreground}" Data="M0,2 L0,4.8 L2.5,7.4 L7.1,2.8 L7.1,0 L2.5,4.6 z" FlowDirection="LeftToRight"/>
                                </Grid>

                                <!-- Content for the menu text etc -->
                                <ContentPresenter Grid.Column="1"
                            Margin="{TemplateBinding Padding}"
                            x:Name="HeaderHost"
                            RecognizesAccessKey="True"
                            ContentSource="Header"/>

                                <!-- The Popup is the body of the menu which expands down or across depending on the level of the item -->
                                <Popup IsOpen="{Binding Path=IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Right" x:Name="SubMenuPopup" Focusable="false" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}">
                                    <Border x:Name="SubMenuBorder" BorderBrush="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Menu}}}" BorderThickness="1" Padding="2,2,2,2">
                                        <Grid x:Name="SubMenu" Grid.IsSharedSizeScope="True">
                                            <!-- StackPanel holds children of the menu. This is set by IsItemsHost=True -->
                                            <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle"/>
                                        </Grid>
                                    </Border>
                                </Popup>
                            </Grid>
                        </Border>

                        <!-- These triggers re-configure the four arrangements of MenuItem to show different levels of menu via Role -->
                        <ControlTemplate.Triggers>

                            <!-- If no Icon is present the we collapse the Icon Content -->
                            <Trigger Property="Icon" Value="{x:Null}">
                                <Setter Property="Visibility" Value="Hidden" TargetName="Icon"/>
                            </Trigger>

                            <!-- Using the system colors for the Menu Highlight and IsEnabled-->
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="Role" Value="SubmenuItem"/>
                                    <Condition Property="IsHighlighted" Value="true"/>
                                    <Condition Property="IsEnabled" Value="true"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Foreground" Value="#FF08A5E1"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="Role" Value="SubmenuItem"/>
                                    <Condition Property="IsHighlighted" Value="false"/>
                                    <Condition Property="IsEnabled" Value="true"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Foreground" Value="#FFE1E0E0"/>
                            </MultiTrigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Background" Value="Transparent"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

1 个答案:

答案 0 :(得分:4)

因为您的MenuItem只能对像素进行点击测试,或者它的后代实际渲染。您需要在外边框添加透明背景:

<Border x:Name="Border" Background="Transparent">