You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF .NET应用:父控件鼠标悬停时设置MenuItem文本背景样式

WPF MenuItem 高亮问题

MenuItem IsHighlighted True

我已修改Menu和MenuItem的模板副本作为对应控件的样式,并添加了代码片段中所示的触发器。

当前问题:当光标未直接悬停在MenuItem内的文本上,而是悬停在其父控件区域时,无法让文本背景呈现与父控件一致的高亮颜色。

求解决方向,需要的话可提供更多信息,感谢帮助。


Window.xaml 代码

<Window
x:Class="sdb01.MainWindowView"
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:local="clr-namespace:sdb01"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:userControls="clr-namespace:sdb01.Views.UserControls"
xmlns:viewmodels="clr-namespace:sdb01.ViewModels"
Title="MainWindow"
Width="1000"
Height="450"
d:DataContext="{d:DesignInstance Type=viewmodels:MainWindowViewModel}"
AllowsTransparency="True"
Background="{StaticResource Brush.Dark.Background02.Static}"
MouseLeftButtonDown="Window_MouseLeftButtonDown"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
mc:Ignorable="d">
<WindowChrome.WindowChrome>
    <WindowChrome
        CaptionHeight="0"
        CornerRadius="0"
        GlassFrameThickness="0" />
</WindowChrome.WindowChrome>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="40" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Border Background="{StaticResource Brush.Dark.Background01.Static}">
        <userControls:Titlebar />
    </Border>
<!-- Window Content -->
</Grid>
</Window>

Titlebar.xaml 代码

<UserControl
x:Class="sdb01.Views.UserControls.Titlebar"
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:local="clr-namespace:sdb01.Views.UserControls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:Background="{StaticResource Brush.Dark.Background01.Static}"
d:DesignHeight="50"
d:DesignWidth="800"
mc:Ignorable="d">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <Menu
        Grid.Column="0"
        HorizontalAlignment="Left"
        VerticalAlignment="Center"
        Style="{StaticResource MenuStyle1}">
        <MenuItem Style="{StaticResource MenuItemStyle1}" Header="File">
            <Menu>
                <MenuItem Style="{StaticResource MenuItemStyle1}" Header="Open" />
            </Menu>
            <Menu>
                <MenuItem Style="{StaticResource MenuItemStyle1}" Header="Save" />
            </Menu>
            <Menu>
                <MenuItem Style="{StaticResource MenuItemStyle1}" Header="Close" />
            </Menu>
        </MenuItem>

    </Menu>
    <StackPanel
        Grid.Column="1"
        HorizontalAlignment="Right"
        Orientation="Horizontal">
        <Button
            Name="MinimizeButton"
            Click="MinimizeButton_Click"
            Style="{StaticResource TitlebarButton}">
            <Viewbox>
                <Path
                    Data="{StaticResource MinimizeIcon}"
                    Fill="{StaticResource Brush.Dark.Foreground01.Static}"
                    Stretch="Fill" />
            </Viewbox>
        </Button>
        <Button
            Name="MaximizeButton"
            Click="MaximizeButton_Click"
            Style="{StaticResource TitlebarButton}">
            <Viewbox>
                <Path
                    Data="{StaticResource MaximizeIcon}"
                    Fill="{StaticResource Brush.Dark.Foreground01.Static}"
                    Stretch="Fill" />
            </Viewbox>
        </Button>
        <Button
            Name="CloseButton"
            Click="CloseButton_Click"
            Style="{StaticResource TitlebarButton}">
            <Viewbox>
                <Path
                    Data="{StaticResource CloseIcon}"
                    Fill="{StaticResource Brush.Dark.Foreground01.Static}"
                    Stretch="Fill" />
            </Viewbox>
        </Button>
    </StackPanel>
</Grid>
</UserControl>

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="MenuStyle1" TargetType="{x:Type Menu}">
        <Setter Property="Background" Value="{StaticResource Brush.Dark.Background02.Static}" />
        <Setter Property="FontFamily" Value="{DynamicResource {x:Static SystemFonts.MenuFontFamilyKey}}" />
        <Setter Property="FontSize" Value="{DynamicResource {x:Static SystemFonts.MenuFontSizeKey}}" />
        <Setter Property="FontStyle" Value="{DynamicResource {x:Static SystemFonts.MenuFontStyleKey}}" />
        <Setter Property="FontWeight" Value="{DynamicResource {x:Static SystemFonts.MenuFontWeightKey}}" />
        <Setter Property="Foreground" Value="{StaticResource Brush.Dark.Foreground01.Static}" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Menu}">
                    <Border
                        Padding="{TemplateBinding Padding}"
                        Background="Transparent"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="0"
                        SnapsToDevicePixels="true">
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

<ControlTemplate.Triggers>
    <Trigger Property="IsSuspendingPopupAnimation" Value="true">
        <Setter TargetName="PART_Popup" Property="PopupAnimation" Value="None" />
    </Trigger>
    <Trigger Property="Icon" Value="{x:Null}">
        <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
    </Trigger>
    <Trigger Property="IsChecked" Value="true">
        <Setter TargetName="GlyphPanel" Property="Visibility" Value="Visible" />
        <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
    </Trigger>
    <Trigger Property="IsHighlighted" Value="True">
        <Setter TargetName="templateRoot" Property="Background" Value="{StaticResource MenuItem.Highlight.Background}" />
        <Setter TargetName="templateRoot" Property="BorderBrush" Value="{StaticResource MenuItem.Highlight.Border}" />
    </Trigger>
    <Trigger Property="IsEnabled" Value="False">
        <Setter TargetName="templateRoot" Property="TextElement.Foreground" Value="{StaticResource Menu.Disabled.Foreground}" />
        <Setter TargetName="GlyphPanel" Property="Fill" Value="{StaticResource Menu.Disabled.Foreground}" />
    </Trigger>
    <Trigger SourceName="SubMenuScrollViewer" Property="ScrollViewer.CanContentScroll" Value="false">
        <Setter TargetName="OpaqueRect" Property="Canvas.Top" Value="{Binding VerticalOffset, ElementName=SubMenuScrollViewer}" />
        <Setter TargetName="OpaqueRect" Property="Canvas.Left" Value="{Binding HorizontalOffset, ElementName=SubMenuScrollViewer}" />
    </Trigger>
</ControlTemplate.Triggers>

内容的提问来源于stack exchange,提问作者AAng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 02:47:02