WPF:当控件放在<menu>中时,不会跟踪MouseOver属性

时间:2015-11-12 15:41:34

标签: c# wpf

我的所有控件都使用自定义样式进行修改,并按预期工作,除非这些控件放在菜单中。当它们位于Menu中时,由于某种原因,它们将MouseOver属性覆盖为蓝色突出显示和边框。我在菜单中有一些不寻常的控件,比如CheckBox和Button控件,但是他们仍然应该尊重他们的MouseOver属性。

enter image description here

这是重现问题的最小代码。这里有一个按钮MouseOver在菜单之外的一个方向,但在菜单内部有一个不需要的蓝色突出显示。

<Window x:Class="MyApp.Window1"
        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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyApp"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2" x:Name="myWindow"
        mc:Ignorable="d"
        Title="MyTitle"
        Width="560" Height="180" 
        MinWidth="560" MinHeight="180"
        Background="#FF222222" Foreground="Red" BorderBrush="#FF222222" ResizeMode="NoResize" WindowStyle="None" Opacity="0.75" WindowStartupLocation="CenterOwner">
    <Window.Resources>
        <sys:Double x:Key="height">40</sys:Double>
        <sys:Double x:Key="titleBarSize">36</sys:Double>
        <sys:Double x:Key="fontSize">16</sys:Double>
        <Color x:Key="backgroundColor">#FF222222</Color>
        <Brush x:Key="backgroundColorBrush">#FF222222</Brush>
        <Color x:Key="highlightedColor">#FF333333</Color>
        <Brush x:Key="highlightedColorBrush">#FF333333</Brush>
        <Color x:Key="borderColor">#FF333333</Color>
        <Brush x:Key="borderColorBrush">#FF333333</Brush>
        <Color x:Key="textColor">#FF999999</Color>
        <Brush x:Key="textColorBrush">#FF999999</Brush>
        <Brush x:Key="titleBarColorBrush">#FF222222</Brush>
        <Thickness x:Key="Tab_Border_Thickness">2,2,2,2</Thickness>
        <Thickness x:Key="TabItem_Border_Thickness_Selected">2,2,2,0</Thickness>      

        <!--Button-->
        <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
            <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
            <Setter Property="Background" Value="{StaticResource backgroundColorBrush}"/>
            <Setter Property="BorderBrush" Value="{StaticResource backgroundColorBrush}"/>
            <Setter Property="Foreground" Value="{StaticResource textColorBrush}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Width" Value="Auto"/>
            <Setter Property="Height" Value="{StaticResource height}"/>
            <Setter Property="FontSize" Value="{StaticResource fontSize}"/>
            <Setter Property="Padding" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border x:Name="border" BorderBrush="{StaticResource backgroundColorBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{StaticResource backgroundColorBrush}" 
                                SnapsToDevicePixels="true">
                            <ContentPresenter x:Name="contentPresenter" Focusable="False" RecognizesAccessKey="True" 
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsDefaulted" Value="true">
                                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource highlightedColorBrush}"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" TargetName="border" Value="{StaticResource highlightedColorBrush}"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource highlightedColorBrush}"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter Property="Background" TargetName="border" Value="{StaticResource highlightedColorBrush}"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource highlightedColorBrush}"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Background" TargetName="border" Value="{StaticResource backgroundColorBrush}"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource backgroundColorBrush}"/>
                                <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource textColorBrush}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>

    <Grid>
        <Menu x:Name="menu" HorizontalAlignment="Left" Height="40" VerticalAlignment="Top" Width="560">
            <Button x:Name="button1" Content="button 1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"  Style="{DynamicResource ButtonStyle1}"/>
        </Menu>
        <Button x:Name="button2" Content="button 2" HorizontalAlignment="Left" Margin="10,54,0,0" VerticalAlignment="Top" Width="75"  Style="{DynamicResource ButtonStyle1}"/>
    </Grid>
</Window>

0 个答案:

没有答案