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

发布于 2024-11-26 20:01:08 字数 5408 浏览 1 评论 0原文

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

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

Can someone please tell me why my menu item is "Highlighted" only when I hover over the menu text and not when I hover anywhere on the menu item? Here's my menu item style:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瞄了个咪的 2024-12-03 20:01:08

因为您的 MenuItem 仅可对其或其后代实际渲染的像素进行命中测试。您需要向外边框添加透明背景:

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

Because your MenuItem is only hit-testable for pixels it, or it's descendants, actually renders. You need to add a Transparent background to your outer border:

<Border x:Name="Border" Background="Transparent">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文