c# - 在 WPF 中单击时更改菜单背景

标签 c# wpf menu menuitem

我正在使用下面的代码来设置 Menu 的样式。当 Mouse 位于菜单上 Over 时,我已经能够更改背景。但是我不知道如何在点击它时改变它的背景

<Window x:Class="WpfApplication11.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>

        <SolidColorBrush x:Key="NormalColor">White</SolidColorBrush>
        <SolidColorBrush x:Key="HoverColor">LightGray</SolidColorBrush>
        <SolidColorBrush x:Key="ClickedColor">DarkGray</SolidColorBrush>

        <Style x:Key="{x:Type Menu}" TargetType="Menu">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Menu">
                        <Border x:Name="MainMenu" Background="{StaticResource NormalColor}"
                                BorderBrush="Black"
                                BorderThickness="1">
                            <StackPanel ClipToBounds="True" Orientation="Horizontal" IsItemsHost="True"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="MainMenu" Property="Background" Value="{StaticResource HoverColor}"/>
                            </Trigger>                           
                        </ControlTemplate.Triggers>                        
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Menu Width="30" Height="20" Margin="10, 10, 5, 5" HorizontalAlignment="Left" Background="White">
            <MenuItem Header="_File">
                <MenuItem Header="_New"/>
                <MenuItem Header="_Open"/>
                <MenuItem Header="_Close"/>
            </MenuItem>
        </Menu>
    </Grid>
</Window>

更新: 我已经尝试了以下代码,但它也没有用。

<Trigger Property="IsFocused" Value="True">
    <Setter TargetName="MainMenu" Property="Background" Value="{StaticResource ClickedColor}"/>
</Trigger

最佳答案

您可以尝试使用 IsKeyboardFocusWithin。

<Trigger Property="IsKeyboardFocusWithin" Value="True">
    <Setter TargetName="MainMenu" Property="Background" Value="{StaticResource ClickedColor}"/>
</Trigger>

这是添加了 IsKeyboardFocusWithin 触发器的代码。我将 ClickedColor 更改为紫色,以便更容易看到。我在您的菜单中添加了 Name="mm"。我添加了一个堆栈面板,其中的文本 block 绑定(bind)到 mm.IsFocused 和 mm.IsKeyboardFocusWithin,因此您可以直观地看到它们的设置以及更改时间。

<Window x:Class="WpfApplication11.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>

    <SolidColorBrush x:Key="NormalColor">White</SolidColorBrush>
    <SolidColorBrush x:Key="HoverColor">LightGray</SolidColorBrush>
    <SolidColorBrush x:Key="ClickedColor">Purple</SolidColorBrush>

    <Style x:Key="{x:Type Menu}" TargetType="Menu">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Menu">
                    <Border x:Name="MainMenu" Background="{StaticResource NormalColor}"
                            BorderBrush="Black"
                            BorderThickness="1">
                        <StackPanel ClipToBounds="True" Orientation="Horizontal" IsItemsHost="True"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="MainMenu" Property="Background" Value="{StaticResource HoverColor}"/>
                        </Trigger>
                        <Trigger Property="IsKeyboardFocusWithin" Value="True">
                            <Setter TargetName="MainMenu" Property="Background" Value="{StaticResource ClickedColor}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <Menu Width="30" Height="20" Margin="10, 10, 5, 5" HorizontalAlignment="Left" Background="White" VerticalAlignment="Top" Name="mm">
        <MenuItem Header="_File">
            <MenuItem Header="_New"/>
            <MenuItem Header="_Open"/>
            <MenuItem Header="_Close"/>
        </MenuItem>
    </Menu>
    <StackPanel VerticalAlignment="Center">
        <TextBlock Text="menu.IsFocused:" />
        <TextBlock Text="{Binding ElementName=mm, Path=IsFocused}" />
        <TextBlock Text="menu.IsKeyboardFocusWithin:" />
        <TextBlock Text="{Binding ElementName=mm, Path=IsKeyboardFocusWithin}" />
    </StackPanel>
</Grid>

好的,对于菜单项...试试这个:

<Window x:Class="WpfApplication11.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>

    <SolidColorBrush x:Key="NormalColor">White</SolidColorBrush>
    <SolidColorBrush x:Key="HoverColor">LightGray</SolidColorBrush>
    <SolidColorBrush x:Key="ClickedColor">Purple</SolidColorBrush>

    <Style x:Key="{x:Type Menu}" TargetType="Menu">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Menu">
                    <Border x:Name="MainMenu" Background="{StaticResource NormalColor}"
                            BorderBrush="Black"
                            BorderThickness="1">
                        <StackPanel ClipToBounds="True" Orientation="Horizontal" IsItemsHost="True"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="MainMenu" Property="Background" Value="{StaticResource HoverColor}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <ControlTemplate x:Key="MenuItemControlTemplate1" TargetType="{x:Type MenuItem}">
        <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
            <Grid VerticalAlignment="Center">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/>
                <Path x:Name="GlyphPanel" Data="F1M10,1.2L4.7,9.1 4.5,9.1 0,5.2 1.3,3.5 4.3,6.1 8.3,0 10,1.2z" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Margin="3" Visibility="Collapsed" VerticalAlignment="Center"/>
                <ContentPresenter ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="1" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                <Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="False" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Bottom">
                    <Border x:Name="SubMenuBorder" BorderBrush="#FF999999" BorderThickness="1" Background="#FFF0F0F0" Padding="2">
                        <ScrollViewer x:Name="SubMenuScrollViewer" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
                            <Grid RenderOptions.ClearTypeHint="Enabled">
                                <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                    <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=SubMenuBorder}" Height="{Binding ActualHeight, ElementName=SubMenuBorder}" Width="{Binding ActualWidth, ElementName=SubMenuBorder}"/>
                                </Canvas>
                                <Rectangle Fill="#FFD7D7D7" HorizontalAlignment="Left" Margin="29,2,0,2" Width="1"/>
                                <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle"/>
                            </Grid>
                        </ScrollViewer>
                    </Border>
                </Popup>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsSuspendingPopupAnimation" Value="True">
                <Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/>
            </Trigger>
            <Trigger Property="Icon" Value="{x:Null}">
                <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
            </Trigger>
            <Trigger Property="IsChecked" Value="True">
                <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
                <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
            </Trigger>
            <Trigger Property="IsHighlighted" Value="True">
                <Setter Property="Background" TargetName="templateRoot" Value="#3D26A0DA"/>
                <Setter Property="BorderBrush" TargetName="templateRoot" Value="#FF26A0DA"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="#FF707070"/>
                <Setter Property="Fill" TargetName="GlyphPanel" Value="#FF707070"/>
            </Trigger>
            <Trigger Property="CanContentScroll" SourceName="SubMenuScrollViewer" Value="False">
                <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=SubMenuScrollViewer}"/>
                <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=SubMenuScrollViewer}"/>
            </Trigger>
            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                <Setter TargetName="templateRoot" Property="Background" Value="{StaticResource ClickedColor}" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Window.Resources>
<Grid>
    <Menu Width="100" Height="20" Margin="10, 10, 5, 5" HorizontalAlignment="Left" Background="White" VerticalAlignment="Top">
        <MenuItem Header="_File" Name="mm" Template="{DynamicResource MenuItemControlTemplate1}">
            <MenuItem Header="_New"/>
            <MenuItem Header="_Open"/>
            <MenuItem Header="_Close"/>
        </MenuItem>
        <MenuItem Header="_Edit" Template="{DynamicResource MenuItemControlTemplate1}">
            <MenuItem Header="_Copy"/>
            <MenuItem Header="_Cut"/>
            <MenuItem Header="_Paste"/>
        </MenuItem>
    </Menu>
    <StackPanel VerticalAlignment="Center">
        <TextBlock Text="menuitem.IsFocused:" />
        <TextBlock Text="{Binding ElementName=mm, Path=IsFocused}" />
        <TextBlock Text="menuitem.IsKeyboardFocusWithin:" />
        <TextBlock Text="{Binding ElementName=mm, Path=IsKeyboardFocusWithin}" />
    </StackPanel>
</Grid>

关于c# - 在 WPF 中单击时更改菜单背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24684392/

相关文章:

C# 添加列表/列表到数据类实例

c# - LINQ:没有使用点表示法的 long final 匿名变量的多个 JOINS

wpf - 在 Window.Resources 中的 DataTemplate 中绑定(bind)到拥有窗口的 View 模型中的属性

c# - 以编程方式在 VS2010 项目中添加现有项目?

c# - 类可以在 C# 中使用别名吗?

c# - Prism WPF 动态区域

c# - Count + List with Futures 不起作用

java - 如何动态更改ActionBar中的图标

winapi - 当鼠标在焦点外单击时隐藏/关闭菜单

html - CSS 中有没有一种方法可以让一个子元素指向一个父元素而不是一个元素?