WPF 边框 IsMouseOver 触发器不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15233225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:53:48  来源:igfitidea点击:

WPF Border IsMouseOver trigger not working

wpfxaml

提问by Jaska

I have defined this kind of style in app.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>

And I'm trying to use it in another xaml -file like this:

我正在尝试在另一个 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>

But nothing happens when I mouse over the border.. what could be wrong here?

但是当我将鼠标悬停在边界上时什么也没有发生……这里有什么问题?

回答by sa_ddam213

Its because you have set the Backgroundin the Border, this will override the Style

因为您在 中设置BackgroundBorder,这将覆盖Style

You will have to remove Background="#FF801F1F"from the Borderxamlso the Stylecan set the Background

您必须从 中删除Background="#FF801F1F"Borderxaml以便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>