<menu>中的对象覆盖了MouseOver边框

时间:2015-11-07 20:33:24

标签: c# wpf

我在“菜单”容器中有不寻常的项目,如按钮,ComboBox,CheckBox等。问题是这些控件的MouseOver边框会以某种方式被覆盖,只有当它们在该容器中时才会被覆盖。在其他任何地方,他们都很好。

每个控件(CheckBoxes,ComboBoxes,Buttons,Labels等)都有自定义样式。除非这些控件放在“菜单”中,否则此自定义样式可以正常工作。当发生这种情况时,由于某些原因,我得到非蓝色边框突出显示非MenuItem对象。如何在不移动或更改其他控件的情况下修复边框颜色?

以下是重现问题的最小代码:

<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 个答案:

没有答案