WPF - 按下按钮时的OuterGlow效果

问题描述 投票:-2回答:1

我在WPF中有一个按钮,当按下按钮时,它将产生一个外部效果。但是,当没有按下按钮时会产生效果,只有按下按钮时才会产生效果?

这是我的XAML:

    <Button.Effect>
    <DropShadowEffect
   BlurRadius="20" ShadowDepth="0" Color="DarkBlue">
    </DropShadowEffect>
</Button.Effect>


</Button>

这是我编辑的代码:

 <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="147,131,0,0" VerticalAlignment="Top" Width="75" PreviewMouseDown="button_PreviewMouseDown">

            <Style x:Name="Triggers" TargetType ="Button">
                <Style.Triggers>
                    <Trigger Property="IsPressed" Value="true">
                        <Button.Effect>
                            <DropShadowEffect
           BlurRadius="20" ShadowDepth="0" Color="DarkBlue">
                            </DropShadowEffect>
                        </Button.Effect>
                    </Trigger>
                </Style.Triggers>
            </Style>


        </Button>

谁能告诉我为什么它不起作用?

编辑:

<menu:HomeButton x:Name="BtnHome" PreviewMouseDown="BtnHome_PreviewMouseDown" TouchDown="BtnHome_TouchDown"   >
                <Button.Style>
                    <Style TargetType="Button">
                        <Style.Triggers>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter Property="Effect">
                                    <Setter.Value>

                                        <DropShadowEffect
       BlurRadius="20" ShadowDepth="0" Color="DarkBlue"/>
                                    </Setter.Value>
                                </Setter>


                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </menu:HomeButton>
c# wpf xaml
1个回答
2
投票

您可以使用Style属性和IsPressed的触发器:

<Button Content="Button">
    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect BlurRadius="20" ShadowDepth="0" Color="DarkBlue"/>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

参考https://stackoverflow.com/a/46279626/6741868

© www.soinside.com 2019 - 2024. All rights reserved.