WPF Border IsMouseOver触发器无法正常工作

时间:2013-03-05 20:14:49

标签: wpf xaml

我在app.xaml中定义了这种样式:

    <Style x:Key="RedCloseButton" TargetType="Border">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Border.Background" Value="Yellow" />
                </Trigger>
                <Trigger Property="IsMouseOver" Value="False">
                    <Setter Property="Border.Background" Value="Black" />
                </Trigger>
            </Style.Triggers>
    </Style>

我正试图在另一个像这样的xaml文件中使用它:

    <Border Style="{StaticResource RedCloseButton}" Name="ClearValue" BorderThickness="2" BorderBrush="black" CornerRadius="0,4,4,0" Margin="110,90,0,80" Background="#FF801F1F">                
            <Rectangle Margin="10,11,6,10" Fill="White" RadiusX="2" RadiusY="2" IsHitTestVisible="False"></Rectangle>
    </Border>

但是当我将鼠标移到边境时没有任何反应......这里可能出现什么问题?

1 个答案:

答案 0 :(得分:16)

因为您在Background中设置了Border,这将覆盖Style

您必须从Background="#FF801F1F" Border移除xaml,以便Style可以设置Background

<Border Style="{StaticResource RedCloseButton}" Name="ClearValue" BorderThickness="2" BorderBrush="black" CornerRadius="0,4,4,0" Margin="110,90,0,80">                
        <Rectangle Margin="10,11,6,10" Fill="White" RadiusX="2" RadiusY="2" IsHitTestVisible="False"></Rectangle>
</Border>